无涯教程-Clojure - 可变函数

65 阅读1分钟

可变参数函数是带有不同数量参数的函数(某些参数是可选的)。 函数也可以指定“&”符号以接受任意数量的参数。

(defn demo 
   [message & others]
   (str message (clojure.string/join " " others)))

上面的函数声明在参数arother旁边有一个“&”符号,这意味着它可以接受任意数量的参数。

如果您将上述函数调用为

(demo "Hello" "This" "is" "the" "message")

以下将是输出。

HelloThis is the message

" clojure.string/join" 用于合并每个单独的字符串参数,该参数传递给函数。

参考链接

www.learnfk.com/clojure/clo…