Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.

Topic: INFINITE SETS IN PROLOG
Replies: 4   Last Post: Feb 25, 2013 7:18 PM

 Messages: [ Previous | Next ]
 Graham Cooper Posts: 4,048 Registered: 5/20/10
INFINITE SETS IN PROLOG
Posted: Feb 23, 2013 5:25 PM

nat(0).
nat(s(X)) :- nat(X).

even(0).
even(s(s(X))) :- even(X).

odd(s(0)).
odd(s(s(X))) :- odd(X).

*** e is SET MEMBERSHIP ***

e(A,nats) :- nat(A).
e(A,evens) :- even(A).
e(A,odds) :- odd(A).

insect(S1,S2) :- e(A,S1), e(A,S2).

?- insect(nats,evens).
?- insect(nats,odds).

This will return TRUE TRUE

******************************

You can see the results of the Set Intersection.

?- e(A,nats) , e(A,evens)

will return

A = 0
A = s(s(0))
A = s(s(s(s(0))))
A = s(s(s(s(s(s(0))))))

*******************************

?- e(A,nats) , e(A,odds)

will return

A = s(0)
A = s(s(s(0)))
A = s(s(s(s(s(0)))))
A = s(s(s(s(s(s(s(0)))))))

NOTE:

?-insect( odds, evens ) will CRASH!

HOMEWORK:

Define divby3(X)
and use it to output all numbers divisible by 6!

Herc
--
www.BLoCKPROLOG.com

Date Subject Author
2/23/13 Graham Cooper
2/25/13 BruceS
2/25/13 Graham Cooper
2/25/13 BruceS
2/25/13 Graham Cooper