變量函數(shù)是采用不同數(shù)量參數(shù)的函數(shù)(一些參數(shù)是可選的)。 函數(shù)還可以指定“&”符號(hào)符號(hào)以接受任意數(shù)量的參數(shù)。
下面的例子顯示了如何實(shí)現(xiàn)這一點(diǎn)。
(defn demo [message & others] (str message (clojure.string/join " " others)))上面的函數(shù)聲明在參數(shù)other的旁邊有'&'符號(hào),這意味著它可以接受任意數(shù)量的參數(shù)。
(demo "Hello" "This" "is" "the" "message")
以上示例將輸出以下結(jié)果:
“HelloThis is the message”
'clojure.string / join'用于組合每個(gè)單獨(dú)的字符串參數(shù),它被傳遞給函數(shù)。
更多建議: