Scheme: Why is there the need to use a cond here?
-
21-09-2019 - |
Question
I tried to write a (simple, i.e. without eqan?
) one?
function like such:
(define one?
(lambda (n)
((= 1 n))))
But the above doesn't work though because when I call it like such:
(one? 1)
I get greeted with this error:
procedure application: expected procedure, given: #t (no arguments)
The correct way (from The Little Schemer) to write it is:
(define one?
(lambda (n)
(cond
(else (= 1 n)))))
Why is there a need to use a cond
with an else
clause, instead of just returning (= 1 n)
?
Solution
There isn't any reason why you would want to do that. I'll check my copy of TLS when I get home to see if I can divine what's going on, but you're not missing anything fundamental about cond
or anything.
Response to your note above: It's not working because you have an extra set of parentheses in the body of the lambda. It should be
(lambda (n) (= 1 n))
The extra parentheses in your version mean that instead of returning the value #t
or #f
, you're trying to call that value as a function with no arguments.
OTHER TIPS
not having a copy of The Little Schemer handy, your example looks as if should work. I think the cond
is extraneous. In psudeo-C the equivant (with cond) is:
int
one(int n)
{
switch (foo) {
default:
return 1 == n;
}
}