This exact use case is covered by letfn, which creates a named fn
accessible to all function definitions and the body. That even allows
mutual recursive definitions without declare. Your example would be
(defn fib-n [n]
(letfn [(fib [a b]
(cons a (lazy-seq (fib b (+ b a)))))]
(take n (fib 1 1))))
Note that its grammar is
(letfn [fnspecs*] exprs*)
fnspec ==> (fname [params*] exprs)
That is, don't forget to surround a function definition with parentheses as
above, and not as
(letfn [fib [a b] ...])
CompilerException java.lang.IllegalArgumentException: Don't know how to
create ISeq from: clojure.lang.Symbol
The reason is that letfn accepts multiple definitions, and as each function
can have multiple expressions as in a do form, you can't just partition the
vector as you do in let.
On Thursday, August 29, 2013 4:32:00 PM UTC-3, Jim foo.bar wrote:
>
> On 29/08/13 20:23, JvJ wrote:
> > I wonder if the somewhat counterintuitive concept of a "named
> > anonymous function" eludes some people, or isn't properly conveyed in
> > tutorials.
>
> I only consider #(...) as an anonymous function. The longer form (fn []
> (...)) has the potential of being perfectly named, it's just you who
> doesn't give it a name usually...
>
> Jim
>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.