CS152
Chris Pollett
Nov 29, 2021
| ?-loves(Z,Y).
Z=jane Y=seagull
[] [a,b,c] [dogs,cats,marbles,mix] [root, [l1, l2], [l3]]
?- nth0(1,[a,b,c], X). X = b.
append([],L,L). append([X|L1],L2,[X|L3]) :- append(L1,L2,L3).
| ?- append([a,b],[c],Z). Z = [a,b,c]or
| ?- append(Y,[e,f,g],[a,b,c,d,e,f,g]). Y = [a,b,c,d]
| ?- findall(X, bird(X), Z). Z = [ostrich, penguin, seagull, eagle].
Which of the following statements is true?
a1 ∧ a2 ∧ ... an → b1 ∨ b2
var(X) -
succeeds if X is an uninstantiated variable
atom(X) -
succeeds if X is currently stands for an atom.
integer(X) -
succeeds if X is currently stands for an integer.
real(X) -
succeeds if X is currently stands for a real.
is_list(X) -
succeeds if X is currently stands for a list.
X=Y -
succeeds if X and Y are instantiated to the same value.
Uninstantiated variables can be equal to anything.
X\=Y -
succeeds if X and Y are not instantiated to the same value.
X==Y -
like X=Y but can only succeed if all varibles instantiated
X\==Y -
succeeds if X == Y fails.
X < Y or X >Y or X >= Y or X =< Y
succeed if X and Y are numbers and the corresponding relation holds.
add(X,Y,Z) :- Z=X+Y.
new_add(X,Y,Z) :- Z is X+Y.
atom_string(?Atom, ?String) - Allows you to bidrectionally convert between atoms and strings number_string(?Number, ?String) - Allows you to bidirectionally convert between numbers and strings string_chars(?String, ?Chars) - Allows you to convert between strings and a list of chars (not their codes) string_length(+String, -Length) - can be used to get the length of a string string_concat(?String1, ?String2, ?String3) is satisfied if String1 concatenated with String2 is String3 split_string(+String, +SepChars, +PadChars, -SubStrings) - useful for splitting a string according to SepChars into a list of Substrings string_upper(+String, -UpperCase) / string_lower(+String, -LowerCase) - useful for changing case of a string
display(X) -
Prints X to the current output device (usually screen which is called userout).
nl -
Write a newline to the current output device.
put(X) -
prints a character X given as an ASCII integer to the current output device.
tab(X) -
Insert X spaces on current line of current output device.
write(X) -
Prints X to the current output device. Write pays attention to whether or not an operator
should be in infix notation, display ignores this information.
get(X) -
gets the next ASCII character of value >32 from the current input device.
(On Unix systems have to watch out since buffering goes on even when input
device is the keyboard.)
get0(X) -
get next character from current input device.
read(X) -
reads a term followed by a period from current input device. the variable X is bound only
to the term and not the period
skip(X) -
skip over X many characters from input device.
see(X) -
set the file X to be the current input device. X=userin is the keyboard. I.e, see
sets current input device to X.
seeing(X) -
if X is a variable then X instantiated to name of current input device.
seen -
close file return current input device to keyboard.
tell(X) -
set the current output device to be the file X. The screen is the output device userout.
telling(X) -
if X is a variable then X instantiated to name of current output device.
told -
close file return current output device to screen.
trace -
Put Prolog in mode where each step of Prolog search printed out.
notrace -
Takes Prolog out of trace mode.
spy P -
Put a spy point on a predicate P. For example | ?- spy dog/2. would put a spy point on the 2-ary predicate dog.
nospy P -
Removes a spy point from a predicate P.
debugging -
turns on debugging mode so that whenever spied-on predicates are called their information is displayed.
nodebug -
turn off debugging mode.
arg(N, T, A) -
evaluates to true if and only if A is the Nth argument of T
For example, arg(3, foo(a,b,c), A) returns A = c
asserta(X) -
adds the clause X to the start of Prolog's database of clauses.
assertz(X) -
adds the clause X to the end of Prolog's database of clauses.
clause(H, B) -
evaluates to true if there is a rule in the Prolog database with head H and body B.
=.. -
is used to equate a term with a list. For example
?- T .. [foo, a, b, c]. gives
T = foo(a, b, c).
functor(Term, Functor, Arity) -
evaluates to true if and only if the first argument is a
term whose functor (top function) is Functor of arity Arity. For example,
functor(foo(a,b,c), foo, 3) evaluates to true
functor( foo(a,b,c), X, Y) would return X = foo, Y=3
retract(X) -
delete first clause which matches X from prolog database of clauses.
Only works on dynamically created clauses.
call(X) -
succeeds if goal X succeeds.
not(X) -
succeeds if goal X fails.
?- functor(T, foo, 3), arg(1, T, a), arg(2, T, b), arg(3, T, x). T = foo(a, b, c).
param_loop (L,H, F) :- natural(I), I >= L, G =.. [F, I], call (G), I = H, !.The goal param_loop(5, 10, write). would output
5678910 trueHere ! stops backtrack after I=H is satisfied. (We'll talk more about ! later.)
a :- b,c,d,!,e,f,g.
not(X) :- call(X),!,fail. not(_).
hd_of_state(bob,elbonia) :- !. hd_of_state(anne,outland) :- !. hd_of_state(gilbert,mordor) :- !. hd_of_state(sally,frisia) :- !.