一个匿名函数在clojure中需要多少个参数?

一个匿名函数在clojure中需要多少个参数?,clojure,arguments,anonymous-function,Clojure,Arguments,Anonymous Function,Clojure如何确定一个匿名函数(使用#…符号创建)需要多少个参数 您需要引用带有%1、%2等的参数,以使函数需要这么多参数。#(println“Hello,world!”)->无参数 #(println(str“Hello,%”!”)->1个参数(%是%1的同义词) #(println(str%1“,“%2”!”)->2个参数 等等。请注意,您不必使用所有的%ns,预期参数的数量由最大的n定义。所以#(println(str“Hello,”%2))仍然需要两个参数 您还可以使用%&捕获res

Clojure如何确定一个匿名函数(使用
#…
符号创建)需要多少个参数


您需要引用带有%1、%2等的参数,以使函数需要这么多参数。

#(println“Hello,world!”)
->无参数

#(println(str“Hello,%”!”)
->1个参数(
%
%1
的同义词)

#(println(str%1“,“%2”!”)
->2个参数

等等。请注意,您不必使用所有的
%n
s,预期参数的数量由最大的n定义。所以
#(println(str“Hello,”%2))
仍然需要两个参数

您还可以使用
%&
捕获rest参数,如中所示

(#)(println“Hello”(应用str(interpose)和“%&”)“Jim”“John”“Jamey”)

从:


它给出了一个错误,即您向匿名函数传递了一个预期为零的参数

匿名函数的算术性由内部引用的最高参数确定。

e、 g

(#(标识[2])
->必须传递算术0,0参数

(#(标识[%1])14)
->必须传递算术1,1参数

(#(identity[%])14)
->(
%
%1
的别名,当且仅当arity为1时),必须传递1个参数

(#(标识[%1%2])14 13)

(#(标识[%2])14 13)
->arity 2,必须传递2个参数


(#(identity[%&])14)
->arity n,可以传递任意数量的参数

在前3个代码示例中,在println周围加括号有什么意义?带有
%&
的酷而简洁的示例。谢谢
user=> (#(identity [2]) 14)
java.lang.IllegalArgumentException: Wrong number of args (1) passed to: user$eval3745$fn (NO_SOURCE_FILE:0)
Anonymous function literal (#())
#(...) => (fn [args] (...))
where args are determined by the presence of argument literals taking the 
form %, %n or  %&. % is a synonym for %1, %n designates the nth arg (1-based), 
and %& designates a rest arg. This is not a replacement for fn - idiomatic 
used would be for very short one-off mapping/filter fns and the like. 
#() forms cannot be nested.