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

From: Greg Woodhouse (gregory.woodhouse at gmail.com)
Date: Tue Dec 2 18:34:03 EST 2008

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!)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20081202/76eb3c0e/attachment.html>

Posted on the users mailing list.