嵌套匿名函数是否合法?我为4clojure 的问题#107写了以下内容:
(fn [n] #(reduce * (repeat n %)))
Run Code Online (Sandbox Code Playgroud)
它通过了所有3个测试,但是当我在repl中尝试使用测试3时,我得到一个IllegalStateException,说不允许嵌套#():
IllegalStateException Nested #()s are not allowed
clojure.lang.LispReader$FnReader.invoke (LispReader.java:628)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: n
in this context, compiling:(NO_SOURCE_PATH:1:44)
RuntimeException Unmatched delimiter: )
clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: )
clojure.lang.Util.runtimeException (Util.java:221)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: %
in this context, compiling:(NO_SOURCE_PATH:0:0)
RuntimeException Unmatched delimiter: )
clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: )
clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: )
clojure.lang.Util.runtimeException (Util.java:221)
RuntimeException Unmatched delimiter: )
clojure.lang.Util.runtimeException (Util.java:221)
Run Code Online (Sandbox Code Playgroud)
为什么这个不能通过repl而不是4clojure传递?
Mag*_*gos 13
嵌套的匿名函数完全可以.但你不能嵌套#()读取器宏,因为它没有很好地定义 - 我们不知道是否%1程序员是否意味着外部函数文字的第一个参数或内部函数文字的第一个参数.
fn如果要评估整个测试表单,则需要输出'longhand'(使用)内部函数.
(fn [n]
(fn [m]
(reduce * (repeat n m))))
Run Code Online (Sandbox Code Playgroud)
在4Clojure上工作必须是因为他们在将其插入测试表单之前评估您提供的表单.因此,在fn*评估测试表单时,#()宏已经扩展(到a ).