






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Today ...
Output
?- write(hi). hi true.
?- write(’Hello World!’). Hello World! true.
?- write(’Hello’), nl, write(’World!’). Hello World! true.
Input
?- read(X). |: hello. % |: is the default read prompt X = hello.
?- read(X). |: ’Hello World!’. % Must be quoted here X = ’Hello Word!’.
?- prompt(_, ’> ’). % first arg is old prompt true.
?- read(X).
hi. X = hi.
?- write(’Please enter something: ’), read(X). Enter somthing: hi. X = hi.
Prolog supports various other I/O operations as well
Prolog makes some I/O tasks easy
menu_choice(X) :- write(’Enter a choice (1 to 4): ’), read(X), member(X, [1,2,3,4]).
Instead, use the Prolog cut (!) operator
A very simple example
a :- b, c. a :- d. b. c. d.
Q: What is the proof tree for solve[a]?
?- a. true ; true.
Now lets add a cut ...
a :- b, !, c. a :- d. b. c. d.
solve [a]
solve [b,!,c]
nothing nothing^ solve [!,c]
solve [c]
nothing nothing nothing solve []^ nothing
We could also rewrite this as(∗):
member(X, [X|_]). member(X, [H|T]) :- + (X=H), member(X, T).
Q: But can you see a possible problem using this or cuts?
Q: For example, what will member(X, [1, 2]) return?
(∗) (^) In fact, + is defined using a cut:
+(P) :- call(P), !, fail. +(P).