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-22 9:49

>>50
When you finish a function, execution has to go back to whoever called it to continue the program. For something like recursion, this could build up a huge list of promises to get back to the calling function, e.g, o+ needs to return to o+ needs to return to o+ needs to return to o+ needs to return to REPL. But if all you're going to do is return to the REPL, then there's no point in keeping around this long chain of recursive calls. When your function is properly tail recursive, it is possible for the compiler to make this happen.

Contrast this to the book's solution. The functions have to return to their caller because the caller has more work to do (add1 in this case). Your solution meant there was nothing left for the function to do.

I do not understand lazy evaluation.
Scheme is generally eager in the sense that all arguments to functions are evaluated prior to the function call. So you type
(foo (+ 3 4) (* 12 12) (factorial 1000))
And scheme goes through the motions
(foo 12 144 ......)
But it may be that foo never actually needs the value of 1000!. Maybe it uses it only when the first argument is not prime or whatever. In that case we wasted all this time and memory calculating (factorial 1000). Lazy evaluation delays the calculation of this until it is needed. If it is never needed, it is never calculated.

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