Problem 1.
An fg-expression is a function term in which f and g are the only function symbols (other than constants). For example, f(a, b), f(a, g(b, c, d)) and g(f(a,b),g(a,b,c),e) are all fg-expressions. In this problem, you may assume that f is binary (takes two arguments), that g is ternary (has three arguments), and that an fg-expression contains no numbers or variables. You may also assume that your functions can only be given fg-expressions, i.e., no error checking is necessary.
Define a Prolog predicate leftmost(X,C)
that is true iff C is the leftmost constant symbol
in the fg-expression X.
Here is a sample terminal session:
| ?- leftmost(f(a, b), C).
C = a
| ?- leftmost(f(m, g(n, p, q)), C).
C = m
| ?- leftmost(g(f(a,b),g(a,b,c),e),C).
C = a
| ?- leftmost(g(f(f(d,e),e),b,c),C).
C = d
Write a Prolog program that counts the number of
f's and the number of g's
in an fg-expression. That is, define
a predicate fgcount(X,F,G) that is true iff F
is the number of f's in X and G is the number of
g's in X, where X is an fg-expression.
Here is a sample terminal session:
| ?- fgcount(g(a,b,c),F,G).
F=0
G=1
| ?- fgcount(f(a,b),F,G).
F=1
G=0
| ?- fgcount(f(g(a,b,c),g(a,b,c)),F,G).
F=1
G=2
Define a Prolog predicate sub(X,Y) that
is true iff Y is the result of
changing f to g and g
to f in X, where
X is an fg-expression.
Here is a sample terminal session:
| ?- sub(f(a,b),Y).
Y = g(a,b)
| ?- sib(f(a,g(a,b,c)),Y).
Y = g)a,f(a,b,c))
Problem 2. The built-in Prolog predicate name(Word,List)
is true iff List is a list of ASCII codes,
one code for each letter in Word. Name
can be used to break a word into a list of ASCII codes, and vice
versa. For example,
| ?- name(bill, List).
List = [98,105,108,108]
| ?- name(Word ,[98,105,108,108]).
Word = bill
Using name, define the predicate letters(Word,List),
which is true iff List is a list of the letters
that make up Word. Here is a sample terminal session:
| ?- letters(bill,List).
List = [b,i,l,l]
| ?- letters(Word,[b,i,l,l]).
Word = bill
Problem 3. Do the following problems from Sethi:
11.3
11.4
11.6
11.10