Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

letrec

18 views
Skip to first unread message

rhat

unread,
Feb 7, 2005, 6:45:49 PM2/7/05
to
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?

Edi Weitz

unread,
Feb 7, 2005, 6:50:40 PM2/7/05
to

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 "spam...@agharta.de" 5) "edi")

rhat

unread,
Feb 7, 2005, 11:41:41 PM2/7/05
to
Intresting, I would have never associated the two names: "letrec" and
"labels"

Thanks for the speedy reply,
Ryan

0 new messages