views:

120

answers:

2

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) ?

+6  A: 

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.

mquander
*page 79* (4th Edition)
Andreas Grech
+1 yup, the problem was because I had the extra parenthesis; thanks for the explanation.
Andreas Grech
...oh no, I just realized that if I had looked 5cm down the page before asking the question, I would have seen the `one?` function without the `cond` on the same page of the book; lesson learnt.
Andreas Grech
I saw that just now myself :)
mquander
A: 

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;
    }
}
Andrew