letfn - mutually recursive local functions

815 views
Skip to first unread message

Rich Hickey

unread,
Feb 28, 2009, 7:38:46 PM2/28/09
to Clojure
I've added letfn, which lets you define mutually recursive local
functions a la CL's labels.

(defn ring [n]
(letfn [(a [n] (if (zero? n) n (b (dec n))))
(b [n] (if (zero? n) n (c (dec n))))
(c [n] (if (zero? n) n (a (dec n))))]
(c n)))

(ring 1000)

Note this is still subject to stack limits, i.e. no TCO, so please
don't report your StackOverflowErrors. Useful for bounded data only.

Rich

Chouser

unread,
Feb 28, 2009, 8:54:39 PM2/28/09
to clo...@googlegroups.com

This does work quite nicely with trampoline, though:

(letfn [(a [n] #(if (zero? n) n (b (dec n))))
(b [n] #(if (zero? n) n (c (dec n))))
(c [n] #(if (zero? n) n (a (dec n))))]
(trampoline c 100000))

--Chouser

Konrad Hinsen

unread,
Mar 1, 2009, 4:18:44 PM3/1/09
to clo...@googlegroups.com
On 01.03.2009, at 01:38, Rich Hickey wrote:

> I've added letfn, which lets you define mutually recursive local
> functions a la CL's labels.
>
> (defn ring [n]
> (letfn [(a [n] (if (zero? n) n (b (dec n))))
> (b [n] (if (zero? n) n (c (dec n))))
> (c [n] (if (zero? n) n (a (dec n))))]
> (c n)))

I noticed that letfn does not permit destructuring in its argument
lists:

(letfn [(a [[f & r]] f)]
(a [1 2 3]))

java.lang.IllegalArgumentException: fn params must be Symbols
(NO_SOURCE_FILE:2)

Is this intentional?

Konrad.

Rich Hickey

unread,
Mar 1, 2009, 6:01:03 PM3/1/09
to clo...@googlegroups.com

Fixed in svn 1317 - thanks for the report.

Rich


David Sletten

unread,
Mar 10, 2009, 6:19:53 AM3/10/09
to clo...@googlegroups.com

Do you advocate usage of 'letfn' and 'let' similar to the distinction
between LABELS and FLET in CL? In other words, FLET implies non-
recursive local function definition whereas LABELS suggests
(mutually) recursive function(s).

So,
(letfn [(f [x] (+ x 2))] (f 8)) ; LABELS
should ideally be:
(let [f (fn [x] (+ x 2))] (f 8)) ; FLET

Aloha,
David Sletten

Reply all
Reply to author
Forward
0 new messages