Re: simple list q's
From: Martin Sondergaard (nobody_at_nowhere.com)
Date: 06/02/04
- Next message: Remko Troncon: "comp.lang.prolog Frequently Asked Questions"
- Previous message: vannoord_at_let.rug.nl: "Re: internet programming in SICStus"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 2 Jun 2004 20:48:25 +0100
You were using "is" the wrong way round.
You used this :
H mod 2 is 0.
When you use "X is Y", it evaluates an expression Y,
and matches the result with X.
The term X is not evaluated.
So "H mod 2 is 0" makes it match "0" with the term "mod(H, 2)", which fails.
To evaluate "H mod 2" and compare it with 0, you could have used this :
0 is H mod 2.
Your code would use it like this :
% base case
printeven([]):-
write('List empty.').
%
% recursive case
printeven([H|T]):-
0 is H mod 2,
write(H),write(', '),
printeven(T).
%
printeven([_|T]):-
printeven(T).
-- Martin Sondergaard, London.
- Next message: Remko Troncon: "comp.lang.prolog Frequently Asked Questions"
- Previous message: vannoord_at_let.rug.nl: "Re: internet programming in SICStus"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]