Clojure Syntax – Why Nested Shorthand Functions Can’t Be Written

clojurefunctional programmingfunctionssyntax

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 (LispReader.java:630)
...and a bunch of other garbage

Best Answer

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.