[plt-scheme] Re: a few questions

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sun Dec 4 18:10:54 EST 2005

Gregory Woodhouse wrote:
> 
> On Dec 4, 2005, at 2:36 PM, Carl Eastlund wrote:
> 
>> If I recall correctly, LABELS is an old form of letrec.
> 
> 
> Hmm...Trying to  (somewhat mechanically) translate
> 
> (DEFINE FACT
>           (LAMBDA (N)
>                   (LABELS ((L1 (M ANS)
>                                (LP N 1)))
>                           (LP (LAMBDA (M ANS)
>                                       (IF (= M 0) (RETURN ANS)
>                                           (L2 M ANS))))
>                           (L2 (LAMBDA (M ANS)
>                                       (LP (- M 1) (* M ANS))))
>                           (L1 NIL NIL))))
...
> (I don't know what RETURN is, but I assume it's an escape, and so I  
> would have thought replacing (RETURN ANS) with just ans would work,  or 
> at least be syntactically valid.)

You forgot a parenthesis before the lambda in the lp-clause:

(define fact
      (lambda (n)
        (letrec ([l1 (lambda (m ans)
                       (lp n 1))]
                 [lp (lambda (m ans)
                       (if (= m 0) ans
                           (l2 m ans)))]
                 [l2 (lambda (m ans)
                       (lp (- m 1) (* m ans)))])
          (l1 '() '()))))

(Note: [] and () mean the same)

-- 
Jens Axel Søgaard




Posted on the users mailing list.