user> (defn foo ([x] x) ([x y] (+ x y)) ([x y z] (+ x y z)))
#'user/foo
user> (foo 1)
1
user> (foo 1 2)
3
user> (foo 1 2 3)
6
Similarly, you can make a var arg function with the
&
notation.
user> (defn bar ([x] x) ([x & rest-args] (reduce + (cons x rest-args))))
#'user/bar
user> (bar 1)
1
user> (bar 1 2 3 4 5 6 7)
28
The last option for parameter passing is keywords, the parameter list becomes a map. This is discussed in more detail here.
No comments:
Post a Comment