Next: Back to Lists
Up: No Title
Previous: Dealing with NOT
Consider the definition of check numbers from a previous lecture.
Another important definition, is of the factorial function :
0! = 1
n! = n*(n-1)!
factorial(0,1).
factorial(N,F) :-
N > 0,
N1 is N-1,
factorial(N1, F1),
F is N * F1.
- Again, the boundary condition is given first, followed by the
definition of the rule
- The first check is on the input arguments, and validates the calling
arguments
- the repeated call to factorial is then made with a slightly smaller
parameter
Possible to extend the definition of our racers with the use of a
third argument, which encapsulates time - therefore
better(X,Y,T) :-
before(X,Y,T).
better(X,Y,T) :-
before(X, Z, T1),
better(Z, Y, T2),
T is T1 + T2.
- Ordering in the 3rd goal above is important, because T cannot
be evaluated before T1 and T2 have been instantiated
- We can also perform other arithmetic operations to achieve
more statistics about the race, done in the same way as the second
definition of better
Omer F Rana
Fri Feb 14 20:23:31 GMT 1997