logo
  • 教程列表
  • SO官方文档
  • 功能
    • 定义功能
    • 参数和 Arity
    • 定义可变参数函数
    • 定义匿名函数
  1. StackOverflow 文档
  2. clojure 教程
  3. 功能
  4. 定义可变参数函数

定义可变参数函数

Created: November-22, 2018

可以使用符号 & 在其参数列表中定义 Clojure 函数以获取任意数量的参数。所有剩余的参数都作为序列收集。

(defn sum [& args]
  (apply + args))

(defn sum-and-multiply [x & args]
  (* x (apply + args)))

呼叫:

=> (sum 1 11 23 42)
77

=> (sum-and-multiply 2 1 2 3)  ;; 2*(1+2+3)
12
  • 定义匿名函数
  • 参数和 Arity

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 关于我们
  • 免责声明