LectureA
LectureA
with Prolog
As predicates (relations):
presidentof(marySue, iowa).
Prolog programs are constructed from terms:
prime(7).
Constants can be either atoms or numbers:
between(rock,X,hardPlace).
• Atoms: Strings of characters starting with a
lower-case letter or enclosed in apostrophes.
• Numbers: Strings of digits with or without a As structured objects similar to records:
decimal point. computer(name(herky), locn('MLH 303'),
make('IBM'), model('RS6000'))
Variables are strings of characters beginning list(3, list(5, list(8, list(13, nil))))
with an upper-case letter or an underscore.
Appendix A 1 Appendix A 2
Example
sublist(S,L) succeeds if and only if the list S is a
sublist of the list L.
sublist([a,b,c], [a,b,c,d,e]) succeeds.
sublist([c,d], [a,b,c,d,e]) succeeds.
sublist([b,d], [a,b,c,d,e]) fails.
Appendix A 5 Appendix A 6
For list algorithms, the basis usually deals with These two cases correspond to the situation
an empty list, certainly the smallest list. where the sublist begins at the start of the list
(Some algorithms for lists do not handle the empty or the sublist begins later in the list, the only
list and so begin with a singleton list, [H]). two possibilities.
This last clause returns the value in the second suffix(S,L) means list S is a suffix of list L
parameter as the answer by unifying the last suffix(S,S).
parameter with that second parameter.
suffix(S,[H|T]) :- suffix(S,T).
%------------------------------------------------------------
sublist(Sb,L) means list Sb is a sublist of list L
sublist(Sb,L) :- prefix(Sb,L).
sublist(Sb,[H|T]) :- sublist(Sb,T).
Appendix A 11 Appendix A 12
take(N,L,NewL) means list NewL consists of the
concat(A,B,C) means list C is the concatenation first N elements of list L
of lists A and B take(0,L,[ ]).
concat([ ], L, L). take(N,[H|T],[H|R]) :- N>0, M is N-1,
concat([H|T], L, [H|M]) :- concat(T, L, M). take(M,T,R).
Factorial:
fac(0,1).
fac(N,F) :- N>0, N1 is N-1, fac(N1,R), F is N*R.
Appendix A 13 Appendix A 14
substitute(X,NX,L,NL) means NL is the list nodups(L,NDL) means NDL is the list containing
resulting from replacing all occurrences of item the items in L with all duplicates removed
X in L by item NX
nodups([ ],[ ]).
substitute(X,NX,[ ],[ ]).
nodups([H|T],L) :- member(H,T),
substitute(X,NX,[X|T],[NX|U]) nodups(T,L).
:- substitute(X,NX,T,U).
nodups([H|T],[H|U]) :- nodups(T,U).
substitute(X,NX,[H|T],[H|U])
:- substitute(X,NX,T,U).
flatten(L,FlatL) means FlatL is the list L with
all atomic items on the top level, for example
spliteven(L,Pre,Suf) means list L is split into a flatten([a,[b,c],[[d],a]], [a,b,c,d,a])
prefix and a suffix of the same length (±1)
Three predefined predicates:
len([ ],0).
atom(A) succeeds if A is an atom.
len([H|T],N) :- len(T,M), N is M+1.
number(N) succeeds if N is a number.
see(F)
diff(L1,L2,Setdiff) means list Setdiff is the set The file whose name is the value of F, an
difference of L1 and L2 atom, becomes the current input stream.
diff([ ],S,[ ]).
seen
diff([H|T],S,R) :- member(H,S), diff(T,S,R).
Close the current input stream.
diff([H|T],S,[H|U]) :- diff(T,S,U).
Appendix A 17 Appendix A 18
write(T)
The Prolog term given by T is displayed on
the current output stream.
tab(N)
N spaces are printed on the output stream.
nl
Newline prints a linefeed character on the
current output stream.
abort
Immediately terminate the attempt to satisfy
original query and return control to top level.
name(A,L)
A is a literal atom or a number, and L is a list
of the ascii codes of the characters
comprising the name of A.
| ?- name(A,[116,104,101]).
A = the
| ?- name(1994,L).
L = [49, 57, 57, 52]
Appendix A 19