11/14

------ cutpr.pl ---------
p(X,Y) :- a(X), b(Y).
p(X,Y) :- a(X), !, b(Y).

p(X,Y) :- X = Y, c(X).

a(1).
a(2).
a(3).

b(1).
b(2).
b(3).

c(1).
c(2).
------------------------

-------- allAns.pl ------------
> bagof(X,q(X,Y,Z), Bag).
> setof(Y,q(X,Y,Z), Bag).
> findall(Y,q(X,Y,Z), Bag). = bagof(X,X^Z^q(X,Y,Z), Bag).
> setof(X,X^Z^q(X,Y,Z), Bag).
> finall(X,(qX,Y,Z), X>1), Bag).
-------------------------------

bagof(Target, Goal, Bag)
setof(Target, Goal, Bag)
findall(Target, Goal, Bag)

bagof(X, q(X,Y,Z), Bag).
Y=1, Z=4, Bag=[3,2]. <- Saying for Y=1, Z=4, the possible values of X are 3 and 2

setof returns a set (no duplicates, ordered)

findall finds all legal values for the target
findall(X, q(X,Y,Z), Bag).
Bag = [1,3,3,1,2].

we can use existential variables to generalize or search.
setof(X, Y^Z^q(X,Y,Z), Bag).
Bag = [1,2,3].
Y^Z^q(X,Y,Z) => means there exists a Y such that there exists a Z such that q(X,Y,Z) is true.
Existential variables can only be used here!


-------- write.pl -----------
test(X) :- write('I noticed you typed '), write(X), nl, !.

factorial(1, 1) :- write(1), !.
factorial(N, F) :- write(N), nl, N1 is N - 1, factorial(N1, F1), F is F1*N.
-----------------------------



Data Structures
 abc, 1.5, 'An Atom', [abc,def,ghi], a(1)
 date (2012,11,14) <- don't need to declare a data type in prolog
 oneYearLater(date(Y,M,D), date(Y1,M,D)) :- Y1 is Y+1.
 getYear (date(Y,_,_), Y).

 pattern matching OK!
 date(2012, M, D) <- matces any 2012 day.


-------- tree.pl ------------
contains(X,tree(X,_,_)).
contains(X,tree(_,R,_)) :- contains(X,R).
contains(X,tree(_,_,L)) :- contains(X,L).
contains(X,leaf(X)).
-----------------------------











Advertisements