[plt-scheme] Re: Named let example from The Guide (sec. 4.6)

From: bullockbefriending bard (kinch1967 at gmail.com)
Date: Tue Dec 2 21:55:02 EST 2008

On Dec 3, 6:34 am, "Greg Woodhouse" <gregory.woodho... at gmail.com>
> The Guide contains th following example illustrating the use of named let:
> (define (duplicate pos lst)
>     (let dup ([i 0]
>               [lst lst])
>      (cond
>       [(= i pos) (cons (car lst) lst)]
>       [else (cons (car lst) (dup (+ i 1) (cdr lst)))])))
>   > (duplicate 1 (list "apple" "cheese burger!" "banana"))
>   ("apple" "cheese burger!" "cheese burger!" "banana")
> I can see that i is initially bound to 0, but [lst lst] looks odd. The
> equivalent letrec
>   (letrec ([proc-id (lambda (arg-id ...)
>                        body ...+)])
>     (proc-id init-expr ...))
> helps, because it shows that lst needs to appear (with some binding) to be
> an argument to th elambda expression bound to dup. At least that's my
> reading.
> I don't use named let much (which is why I was looking it up), and so this
> may not be a good example, but since this is The Guide, I wonder if
> providing an example along the lines of
> (define (factorial n)
>     (let fact ([i 0] [result 1])
>       (if (= i n)
>           result
>           (fact (+ i 1) (* (+ i 1) result)))))
> first might make it seem a bit less mysterious. (Wow, that was all one
> sentence!)
> _________________________________________________
>   For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

I guess this is where 'standard idiom' and 'understanding what you are
doing with a language' bump together.

Presumably new users should be introduced to the concepts of bindings
and shadowing very early on... long before they need to go looking up
how to use a named let.

However, there is a case to be made for some commentary in that
section of the guide saying that with respect to what we assume you
already know about binding in scheme, you will certainly understand
why (let loop ([fred fred]) blah) works and is in fact a common idiom.

That's my $0.02 worth as someone who has taken up Scheme only in the
last 9 months.

Posted on the users mailing list.