Prolog Beginner: Trivial Example that I cannot get to work.
I have some prolog. The lessThanTen and example predicates work as expected however the exam predicate does not work.
lessThanTen(9).
lessThanTen(8).
lessThanTen(7).
lessThanTen(6).
lessThanTen(5).
lessThanTen(4).
lessThanTen(3).
lessThanTen(2).
lessThanTen(1).
lessThanTen(0).
% This should always return 5.
example(X) :-
X is 5.
% This should return all values that ar开发者_高级运维e less than ten.
exam(X) :-
X is lessThanTen(Y).
Here is the output:
% swipl
...
?- [addv1].
Warning: /.../addv1.pl:17:
Singleton variables: [Y]
% addv1 compiled 0.00 sec, 1,484 bytes
true.
?- lessThanTen(X).
X = 9 ;
X = 8 ;
X = 7 ;
...
?- example(X).
X = 5.
?- exam(X).
ERROR: is/2: Arithmetic: `lessThanTen/1' is not a function
?- exam(5).
ERROR: is/2: Arithmetic: `lessThanTen/1' is not a function
I am thinking that the warning I am getting is pretty key.
The is
operator expects its right-hand argument - your lessThanTen(Y)
- to be an arithmetic expression. It, of course, isn't.
Also, exam/1 just looks wrong: what's Y
? The clause takes a single argument X
.
lessThanTen(9).
lessThanTen(8).
lessThanTen(7).
lessThanTen(6).
lessThanTen(5).
lessThanTen(4).
lessThanTen(3).
lessThanTen(2).
lessThanTen(1).
lessThanTen(0).
example(X) :-
X is 5.
exam(X) :-
lessThanTen(X).
精彩评论