From: rhat
Subject: letrec
Date: 
Message-ID: <1107819949.518977.51720@z14g2000cwz.googlegroups.com>
Hi Everyone,
I was just wondering, how do you define a recursive lambda expression?
Lets say I want a factorial function:
(defun fact (n)
    (* n (fact (- n 1))))
In scheme I could say:
(letrec ((temp-fact (lambda (n)
                       (* n (temp-fact (- n 1)))))))

And that would work just fine (give or take a couple parens, since I'm
not using a lisp-friendly editor).

Is there an equivalent expression in Common Lisp?

From: Edi Weitz
Subject: Re: letrec
Date: 
Message-ID: <uy8e0gca7.fsf@agharta.de>
On 7 Feb 2005 15:45:49 -0800, "rhat" <·········@gmail.com> wrote:

> I was just wondering, how do you define a recursive lambda
> expression?
> Lets say I want a factorial function:
> (defun fact (n)
>     (* n (fact (- n 1))))
> In scheme I could say:
> (letrec ((temp-fact (lambda (n)
>                        (* n (temp-fact (- n 1)))))))
>
> And that would work just fine (give or take a couple parens, since
> I'm not using a lisp-friendly editor).
>
> Is there an equivalent expression in Common Lisp?

Sure, LABELS.

  <http://www.lispworks.com/documentation/HyperSpec/Body/s_flet_.htm>

* (labels ((fact (n) (if (zerop n) 1 (* n (fact (1- n))))))
    (fact 100))

93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: rhat
Subject: Re: letrec
Date: 
Message-ID: <1107837700.987586.175920@c13g2000cwb.googlegroups.com>
Intresting, I would have never associated the two names: "letrec" and
"labels"

Thanks for the speedy reply,
Ryan