Re: Users-prolog Digest, Vol 79, Issue 2

Duncan Patton a Campbell <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Organization NeoText
Message-ID <[email protected]>
On Fri, 18 Dec 2009 12:00:25 -0500
[email protected] wrote:

> 
> James Gale wrote:
...

I assuming you want a function that tests whether a number is even or odd?

Such a function is not built into Gprolog, so you will need to construct
one.  exempli gratia:

With  a text editor (vi on *nix, notepad on Wondiws) load edit a file, called
myeven.pl and enter the following line: 

even_number(N) :- 0 is N /\ 1.

Save the file and start the prolog interpreter in the directory containing myeven.pl

gprolog
GNU Prolog 1.3.1
By Daniel Diaz
Copyright (C) 1999-2009 Daniel Diaz
| ?- [myeven].
compiling /tmp/myeven.pl for byte code...
/tmp/myeven.pl compiled, 2 lines read - 366 bytes written, 22 ms

yes
| ?- 

At this point we have loaded the code in myeven.pl into the interpreter
and you can test it so:

| ?- even_number(22).

yes

| ?- even_number(22).

yes
| ?- even_number(-22).

yes
| ?- even_number(3).  

no
| ?- even_number(-223).

no
| ?- even_number(-223.0).
uncaught exception: error(type_error(integer,-223.0),(is)/2)
| ?- 

This last error is because our one line function is factored 
to only deal with integers.

I hope this helps.

Dhu
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.