tags:

views:

45

answers:

2

I have a couple of books on Scheme, and some of them mention named let and letrec, but none will actually give a convincing example of each (I mean, when and why would I use one instead of the other). Are there examples of situations where letrec/named let would be really a better alternative than an internal define, or even an outside auxiliary procedure?

+3  A: 

Which one you use is mostly a matter of style.

I don't find myself using letrec very often, just preferring internal defines. I do use named let quite often, to write tail-recursive loops, similar to this nonsense loop.

(let loop ((var init) (other-var other-init))
  (cond
    ((done? var) var)
    ((finished? other-var) other-var)
    (else (loop (modify var) (manipulate other-var)))))

You could do the same with a letrec or internal define, but I find this one easiest to read.

letrec can be useful when macro-expanding into places in which you don't want to create defines.

Brian Campbell
A: 

Probably the best explanation you can find on the difference between these forms is this.

Vijay Mathew