lecturer(olivier,ai). lecturer(john,pascal). lecturer(geoff,ai). lecturer(john,programming). lecturer(nizam,networks). lecturer(peter,graphics). lecturer(geoff,projects). lecturer(olivier,ppl). lecturer(john,graphics). -------------------------------------------------------- ?- lecturer(Anyone,_). yes Anyone = olivier ? ; yes Anyone = john ? ; yes ...
var(X)
checks for unbound variables.
nonvar(X)
checks for non-unbound variables
atom(X)
checks for atoms
integer(X)
checks for integers
atomic(X)
checks for integers or atoms.
In unifications, the instantiation of variables is as general as possible,
to create a common instance of the two expressions.
For example, p(X,a,Z)
and p(A,B,B)
.
The most common instance is p(X,a,a)
, but p(a,a,a)
is also a common instance.
The rules for unification are :
p(X,X)
with
p(Y,f(Y))
.)
Unification is used between query predicates and clause heads.
The =
predicate determines if two expressions are unifiable,
and \=
the converse.
?- p(a,X,f(Y)) = p(Z,Z,f(b)). yes X = a Y = b Z = a
?-lecturer(geoff,ai) = Job(geoff,ai). yes Job = lecturer== and \==
my_unify
,
which take two arguments.
If the arguments are both atoms, both integers or both variables, attempt
to unify them using the =
predicate, otherwise
my_unify
must fail.
Answer
0
, one by s(0)
, two by
s(s(0))
, etc.
Write Prolog clauses to add, subtract, multiply and divide numbers in
this representation.
For example
?- add(s(0),s(s(0)),X). yes X = s(s(s(0)))Answer
is
predicate
?- X is 2+3+4. yes X = 9
< > >= =<
can be used
to compare evaluable arithmetic expressions.
gcd(X,X,X). gcd(X,Y,GCD):- X < Y, Difference is Y - X, gcd(X,Difference,GCD). gcd(X,Y,GCD):- gcd(Y,X,GCD).
sum
and prod
.
sum
takes three arguments, the first two being operands,
the last being the sum of the first two.
sum
must check that the
first two add to the third.
sum
must add the first two and bind the result to the third.
sum
must perform the subtraction and bind the variable
appropriately.
sum
must fail.
prod
should perform similarly for multiplication and
division.
.
as the functor.
The first argument is the head and the second argument is the tail of
the list.
[]
|
functor and []
brackets are an
alternative to the dot notation.
[
<head>{,
<more head>}[|
<tail list>]]
- i.e. any number of head elements,
optionally followed by a |
and a tail list, all enclosed
in []
numbers([1,2,3,4]). sentence(the,[cat,sat|[on,the,mat]]). ----------------------------------------------------------- ?- numbers([X|Y]). yes X = 1 Y = [2,3,4] ?- numbers([1,Y|Z]). yes Y = 2 Z = [3,4] ?- sentence(the,[Noun|Rest]). yes Noun = cat Rest = [sat,on,the,mat] ?- "ABCDEFG" = .(First,.(Second.[Third,Fourth|Rest])) yes First = 65, Second = 66, Third = 67, Fourth = 68, Rest = [69,70,71]
my_length([],0). my_length([_|Tail],Length):- my_length(Tail,TailLength), Length is TailLength + 1.Three interpretations
L
L
have this length
L
s have this length
my_member(Element,[Element|_]). my_member(Element,[_|Tail]):- my_member(Element,Tail).Three interpretations
E
and element of L
L
E
as an element (a dangerous
interpretation with an infinite number of answers)
my_append([],List,List). my_append([Head|Tail],List,[Head|TailList]):- my_append(Tail,List,TailList).Five interpretations
L1
appended to L2
L1
to get L
L2
to get L
L
list_select(Element,[Element|Tail],Tail). list_select(Element,[Another|Tail],[Another|SelectedTail]):- list_select(Element,Tail,SelectedTail).
insert(Element,List,InsertedList):- list_select(Element,InsertedList,List).
sublist(Sublist,List):- append(_,Back,List), append(Sublist,_,Back).
permutation([],[]). permutation([Head|Tail],PermutedList):- permutation(Tail,PermutedTail), insert(Head,PermutedTail,PermutedList).
%------------------------------------------------------------- %----Counts the number of words in some text, and how often %----each occurs. %----A list of the form [word(Word,Count), ...] is returned. count_words(Words):- %----Read the first word read_word(Word), %----Build the list of words count_words(Word,[],Words). %------------------------------------------------------------- %----Adds words to a list until exit is reached %----At exit return the current list count_words(exit,Words,Words). %----Otherwise add the word too the list. count_words(Word,OldWords,Words):- %----Add the word in the list add_word(Word,OldWords,NewWords), %----Get another word read_word(NextWord), %----Loop count_words(NextWord,NewWords,Words). %------------------------------------------------------------- %----Adds a word to a list. %----If the first word in the list already, increment the %----count add_word(Word,OldWords,[word(Word,NewCount)|OtherWords]):- list_select(word(Word,OldCount),OldWords,OtherWords), NewCount is OldCount + 1. %----If not the first word, then move on down the list add_word(Word,OldWords,[word(Word,1)|OldWords]). %------------------------------------------------------------- %----Read a word from stdin read_word(Word):- %----Prompt user write('Enter word : '), read(Word). %------------------------------------------------------------- %----Standard select code %----Built in Eclipse, so call it something weird list_select(E,[E|T],T). list_select(E,[H|T],[H|TT]):- list_select(E,T,TT). %-------------------------------------------------------------
mega_append
, which has a list of lists as
its first argument and their concatenation (all appended) as the second
argument.
Answer
move(FromPeg,ToPeg)
, returned in the last argument of
hanoi(FromPeg,ToPeg,SparePeg,NumberOfDisks,ListOfMoves)
.
Answer
functor(Term,Functor,Arity)
Term
instantiated, this is used to find/check
the functor and arity of a term.
Term
uninstantiated and the Functor
and Arity
instantiated, this is used to build
Term
s with new variables as arguments.
copy(OldTerm,NewTerm):- functor(OldTerm,Functor,Arity), functor(NewTerm,Functor,Arity).
arg(Position,Term,Argument)
Position
and Term
instantiated to find/check the Position
th argument of the
Term
.
write_each_argument(Term):- functor(Term,_,Arity), write_Nth_argument(Term,1,Arity). write_Nth_argument(Term,N,Arity):- N =< Arity, arg(N,Term,Argument), write(Argument), nl, N1 is N + 1, write_Nth_argument(Term,N1,Arity). write_Nth_argument(_,Arity,Arity).
Term =.. List
(univ)
List
unifies with the functor of
the Term
, and the tail elements of List
unify with the arguments of Term
.
Term
is an atom, the List
is a single
element list.
?- f(a,S,d) =.. L. yes S = S L = [f,a,S,d]
write_each_argument(Term):- Term =.. [_|Arguments], write_arguments(Arguments). write_arguments([]). write_arguments([Argument|RestOfArguments]):- write(Argument), nl, write_arguments(RestOfArguments).
call
in meta-programming section.
name(Atom,List)
Atom
and the List
of the
ASCII values of the characters of the Atom
.
concatenate_atoms(Atom1,Atom2,Result):- name(Atom1,List1), name(Atom2,List2), append(List1,List2,ResultList), name(Result,ResultList).
=..
predicate, for example:
?- flatten(p(X,f(a,b),g(g(g(a)))),Flat). yes Flat = [p, X, f, a, b, g, g, g, a]