### 9.5 Exercises

Exercise  9.1 Which of the following queries succeed, and which fail?

?-  12  is  2*6.

?-  14  =\=  2*6.

?-  14  =  2*7.

?-  14  ==  2*7.

?-  14  \==  2*7.

?-  14  =:=  2*7.

?-  [1,2,3|[d,e]]  ==  [1,2,3,d,e].

?-  2+3  ==  3+2.

?-  2+3  =:=  3+2.

?-  7-2  =\=  9-2.

?-  p  ==  ’p’.

?-  p  =\=  ’p’.

?-  vincent  ==  VAR.

?-  vincent=VAR,  VAR==vincent.

Exercise  9.2 How does Prolog respond to the following queries?

?-  .(a,.(b,.(c,[])))  =  [a,b,c].

?-  .(a,.(b,.(c,[])))  =  [a,b|[c]].

?-  .(.(a,[]),.(.(b,[]),.(.(c,[]),[])))  =  X.

?-  .(a,.(b,.(.(c,[]),[])))  =  [a,b|[c]].

Exercise  9.3 Write a two-place predicate termtype(Term,Type) that takes a term and gives back the type(s) of that term (atom, number, constant, variable, and so on). The types should be given back in the order of their generality. The predicate should behave in the following way.

?-  termtype(Vincent,variable).
yes
?-  termtype(mia,X).
X  =  atom  ;
X  =  constant  ;
X  =  simple_term  ;
X  =  term  ;
no
X  =  complex_term  ;
X  =  term  ;
no

Exercise  9.4 Write a Prolog program that defines the predicate groundterm(Term) which tests whether or not Term is a ground term. Ground terms are terms that don’t contain variables. Here are examples of how the predicate should behave:

?-  groundterm(X).
no
?-  groundterm(french(bic_mac,le_bic_mac)).
yes
?-  groundterm(french(whopper,X)).
no

Exercise  9.5 Assume that we have the following operator definitions.

:-  op(300,  xfx,  [are,  is_a]).
:-  op(300,  fx,  likes).
:-  op(200,  xfy,  and).
:-  op(100,  fy,  famous).

Which of the following are well-formed terms? What are the main operators? Give the bracketings.

X  is_a  witch
harry  and  ron  and  hermione  are  friends
harry  is_a  wizard  and  likes  quidditch
dumbledore  is_a  famous  wizard

© 2006-2012 Patrick Blackburn, Johan Bos, Kristina Striegnitz