views:

64

answers:

2

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 are 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.

+1  A: 

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.

Frank Shearar
Sorry, not quite understanding. Why isn't `lessThanTen(Y)` an arithmetic expression? When I evaluate `lessThanTen(Y)` it is `9;8;7;6;5;4;3;2;1;0.`
sixtyfootersdude
Y is a variable that I created? I basically just want to be able to get all of the values that are less than ten.
sixtyfootersdude
Ok I understand. See my answer for the completed code.
sixtyfootersdude
+1  A: 
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).
sixtyfootersdude
I was going to write pretty much what you did here, but I wasn't sure what exam/1 was meant to do. Glad to see you sorted out your problem!
Frank Shearar
Thanks for the help Frank!
sixtyfootersdude