b. Give an example of a compound term in this language. What are its parts called?
c. Define: arity, functorial form, unification, resolution, backtracking.
d. distinguish "functor" from "function" -- which does Prolog have?
Give examples of
b. Show how 1+(2*3) is stored in Prolog. Show 1+2*3. Show (1+2)*3. Which of these are identical?
c. Draw a UML class diagram of the Prolog data structure.
?- f( [1,2,3], S ).with a Prolog database that has the following facts and rules about f in it:
f( [], 0 ).
f ( [ H | T ], S ) :- f( T, X), S is X + H.
b. Give an example of its effect.
c. Why is this an important limitation?
b. Give good answers to each of the questions generated in part a above.
Show how you would include this data base in Prolog. As examples encode
Table
Company | Cost |
---|---|
'Farmers' | 950 |
'AAA' | 900 |
b. Your current company is costing you 930. Write a query that accesses the data on 'GEICO' and tests the claim that 'GEICO' can save you 15%.
Note: be careful of quotation marks!
All structures in prolog are stored in functorial form. This is a data
structure where every node is an object that has one functor and zero or
more parameters or arguments. The number of parameters is called the arity,
In the above cases:
Table
Expression | Functor | Arity | Parameters |
---|---|---|---|
1 + X | + | 2 | constant 1, variable X |
f(1,X) | f | 2 | constant 1, variable X |
teacher(dick) | teacher | 1 | constant dick |
+X | + | 1 | variable X. |
Here are the object diagrams using the UML:
b. Draw a diagram of 1+(2*3). Show 1+2*3.
(Note. Precedence makes the first two expressions the same.... )
c. Draw a UML class diagram of Prolog data structures.
Note
This is an example of the Composite pattern.
?- f([1,2,3], S).and a Prolog data base has the following facts and rules in it:
f([], 0).
f([ H | T ], S) :- f( T, ST), S is ST+H.
Try it on the computers!
1 ?- trace, f([1,2,3], S).
Here is another form of answer that shows each call and its matching exit:
Call: f([1,2,3], S)
| Call: f([2,3], ST)
| | Call: f([3], ST)
| | | Call: f([], ST)
| | | Exit: f([], 0)
| | | --(ST=0)
| | | Call: S is 0 + 3
| | | Exit: S=3
| | Exit: f([3], 3)
| | --(ST=3)
| | Call: S is 3 + 2
| | Exit: S=5
| Exit: f([2,3], 5)
| --(ST=5)
| Call: S is 5 + 1
| Exit: S=6
Exit: f([1,2,3], 6)
--(S=6)