Nov 3, 2003 ------------- - Imparting more control knowledge in Prolog - cut operator (!) - Many uses of cut - prevent consideration of alternate solutions - "freezing" parts of solutions - by itself evaluates to true - prevent multiple solutions from being produced - Files for today: /* squarelist takes a list of integers and squares every entry */ /* ! is called "cut". It is the goto of Prolog. Use it with great precaution. */ /* cut prevents consideration of alternate solutions by freezing parts of current solution */ /* cut here is doing - fix rule 2 when you know A is an integer - skip all subsequent rules (in this case, rule 3) */ squarelist([],[]). squarelist([A|B],[C|D]) :- integer(A), !, C is A*A, squarelist(B,D). squarelist([A|B],[A|D]) :- squarelist(B,D). /* cut by itself always evalutes to true */ drink(beer). drink(gin). :- ! drink(juice). /* try the above code with the following queries: drink(X). drink(X), drink(Y). drink(X), ?, drink(Y) */ /* cut prevents multiple solutions from being produced. */ member(X,[X|_]) :- !. member(X,[_|Y]) :- member(X,Y). /* max - first two are input arguments, last is the result of max */ /* max(X,Y,X) :- X>=Y. max(X,Y,Y) :- X=Y, !. max(X,Y,Y). */ /* if x>=y then return(x) else return (y) */ /* the nice way to write the max predicate is: */ max(X,Y,X) :- X>=Y, !. max(X,Y,Y) :- X