Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The Little Schemer

Name: Anonymous 2009-07-19 23:23

Hi, I am working through the little schemer. I do not have the math skills to work through SICP.

I have the following code:


(define (member? a lat)
  (lambda (a lat)
    (cond
      ((null? lat) #f)
      (else (or (eq? (car lat) a)
                (member? a (cdr lat)))))))



When I run this say like:
(member? 3 (cons 3 '(9 8 7 6 13 3)))

I get the following:
#<procedure:...my-scheme-log.ss:137:2>
rather than a list without the first instance of 3

Can you help me?

Name: Anonymous 2009-07-21 21:02

Are there any issues that you guys can see with my version vs. the book?
You are performing two tests for almost all cases, while the book is only performing one. In one case, where m is zero, yours performs better, but in all other cases the book solution is faster. This could be improved by checking for this one case at the beginning only, then looping inside, but you probably haven't dealt with named lets or internal defines and such so it's best to let it go for the moment.

On the other hand, your version is properly tail recursive, while the book's solution is not.

Yours is like
(+o 3 2)
=>(+o 2 3)
=>(+o 1 4)
=>(+o 0 5)
=>5


The book's is like
(+o 3 2)
=>(add1 (+o 2 3))
=>(add1 (add1 (+o 1 4)))
=>(add1 (add1 (add1 (+o 0 5))))
=>(add1 (add1 (add1 5)))

etc.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List