[plt-scheme] How is letrec different from letrec* in its use?

From: hendrik at topoi.pooq.com (hendrik at topoi.pooq.com)
Date: Thu Feb 12 23:54:40 EST 2009

On Thu, Feb 12, 2009 at 10:24:24PM -0600, Grant Rettke wrote:
> On Thu, Feb 12, 2009 at 9:58 PM, Sam TH <samth at ccs.neu.edu> wrote:
> > The following program does not have a consistent meaning under all
> > orders of evaluation of the `letrec' right-hand-sides.
> >
> > (letrec ([x 1]
> >            [y (set! x 2)]
> >            [z (set! x (+ x 1))])
> >      x)
> >
> > For example, if we evaluate the bindings top down, we get 3, but if we
> > evaluate z before y, we get 2.  The semantics for `letrec' do not
> > specify which of these you might get (indeed, you might get an error
> > from +).  The semantics of `letrec*' will always give you 3.
> 
> I see.
> 
> Reading the R6RS specification of letrec it says:
> 
> "It should be possible to evaluate each <init> without assigning or
> referring to the value of any <variable>. In the most common uses of
> letrec, all the <init>s are lambda expressions and the restriction is
> satisfied automatically."
> 
> Why letrec is included when it would only work in a predictable manner
> when the <init>s are lambda expressions?

So you could use it in a predicatable manner for lambda expressions, of 
course.  I think, in fact, it was invented first, before letrec*.  I 
think is was in Landin's original ISWIM paper, entitled, "The Next 700 
Progeanning Languages".

-- hendrik


Posted on the users mailing list.