Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to evaluate a Clojure expression with nested shorthand functions today, and it wouldn't let me.

The expression was:

(#(+ % (#(+ % (* % %)) %)) 5) ; sorry for the eye bleed

The output was:

IllegalStateException Nested #()s are not allowed  clojure.lang.LispReader$FnReader.invoke (
...and a bunch of other garbage
share|improve this question
i find that not being able to write such code is a good thing for clojure. – Simon Jan 25 '13 at 11:20
Because it makes your eyes bleed. – Michael Shaw Jan 25 '13 at 13:16
Don't you need (#(+ %1 (#(+ %2 (* %3 %4)) %5)) 5)? – innova Jun 9 '15 at 14:06
up vote 5 down vote accepted

You would know that % belongs to the inner function. The drawback is that you would lose access to the % in the outer function.

Use the fn [x] syntax instead.

share|improve this answer
So? Most of the time I don't need access to the % of the outer fn, and on the times you did, you could fall-back to (fn), right? – Zaz May 12 '15 at 23:56

It's completely arbitrary; there's a couple lines in the parser that explicitly disable it. If you edit that line out, you can have nested anonymous functions, and they act exactly like you'd expect.

specifically, lines 634-635 in

public static class FnReader extends AFn{
    public Object invoke(Object reader, Object lparen) {
        PushbackReader r = (PushbackReader) reader;
        if(ARG_ENV.deref() != null) // <-- line 634
            throw new IllegalStateException("Nested #()s are not allowed");
        // ...
share|improve this answer
Could you identify the line in the parser and demonstrate that the code rewritten to not have an anon nested function and the code with the line removed from the parser and an anon nested function works the same? – user40980 Jan 24 '13 at 22:09
@MichaelT: there you go . and you can just test it; it's easy to do since you can switch them out at runtime . the clojure parser is really quite easily hackable – naiad Jan 25 '13 at 1:17
Well, not completely arbitrary; unless Rick Hickey was just having an arbitrary day, there must have been a reason he put it in there, and you don't seem to know what that reason is. Duh. – Robert Harvey Jan 25 '13 at 19:40
Wow, what a find! Nice -- +1. – user39685 Jan 25 '13 at 20:05
Would this change cause any ambiguous parsings of nested methods? I am curious of a fn [x] rewrite of the OP's code would have identical functionality to a modified clojure version. Furthermore, would there be any issue with portability of the clojure code? – user40980 Jan 25 '13 at 20:52

You can have nested anonymous functions of the (fn [params] (body)) sort. Only the # syntax doesn't support nesting.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.