Held: Monday, April 27, 1998
[]
-- the empty list
[a,b,c]
-- a list of three elements
[a|X]
-- a list whose first element is a and whose
remainder is the list X.
head
. Traditionally, we think of head as
removing the first element of a list. However, in Prolog, we need
to think of it as determining whether something is at the head of
the list (and then use to proof techniques to extract the head of
the list).
head(X,L) :- equal(L,[Y|YS]), equal(X,Y).
head(X,[Y|Ys]) :- equal(X,Y).
head(X,[X|XS]).
tail(XS,[X|XS]).
concatenate
.
concatenate(Xs,Ys,Zs)
holds when Zs
is the
list created by concatenating Xs
and Ys
.
concat([],Ys,Ys).
concat([X|Xs], Ys, [X|Whatever]) :- concat(Xs,Ys,Whatever).
concat([a,b,c], [d,e,f], [a,b,c,d,e,f]).
concat([a,b,c], [d,e,f], [a,b,c,d,e,f]).
concat([a,b,c], X, [a,b,c,d,e,f]). concat(X, [d,e,f], [a,b,c,d,e,f]).
concat(Xs, Ys, [a,b,c,d,e,f]).
member
.
member
might even be able
to extract the members of a list.
node
and
empty
to define trees.
in_tree(Val,empty) :- fail. in_tree(Val,node(Val,Less,Greater)). in_tree(Val,node(NodeVal,Less,Greater)) :- Val < NodeVal, in_tree(Val,Less). in_tree(Val,node(NodeVal,Less,Greater)) :- Val > NodeVal, in_tree(Val,Greater).
insert
and delete
.
display
).
write(X)
predicate always holds and has the side effect
of writing its argument to stdout.
nl
predicate always holds and has the side effect
of writing a newline to stdout.
in_tree
function to list the nodes
it looks at, we might write:
showpath(Val,empty) :- write("not found!"), nl, fail. showpath(Val,node(Val,Less,Greater)) :- write(Val), nl. showpath(Val,node(NodeVal,Less,Greater)) :- Val < NodeVal, write(NodeVal), nl, showpath(Val,Less). showpath(Val,node(NodeVal,Less,Greater)) :- Val > NodeVal, write(NodeVal), nl, showpath(Val,Greater).
Keep track of collections of intermediate "goals" (predicates whose truth/falsity must be determined) Repeatedly Pick a goal (usually the first in the collection) Find a rule that might help solve that goal (usually the first rule whose lhs matches the goal) Determine replacements necessary to match the rule to the goal (variables to be replaced by atoms) Replace the goal in the set with the corresponding preconditions (the stuff to the right of :-, after the substition is made). If you run out of goals, the predicate holds. If you can't solve a goal, "backtrack" to try another rule.
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Thu May 7 20:29:44 1998.
This page generated on Thu May 7 20:34:49 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu