On 11 July 2011 03:42, Luc Prefontaine <[email protected]> wrote:
> Lets try to clear the confusion a bit here:
>
> (def max1 [x] x)
> (def max2 [x y] (if (> x y) x y))
> (def max3 ([x y & more] (reduce max (max x y) more)))
>
> The above a three different fns.
To be even more clear, the above should be:
(defn max1 [x] x)
(defn max2 [x y] (if (> x y) x y))
(defn max3 ([x y & more] (reduce max (max x y) more)))
:)
> max1 has only the arg x.
> max2 has only x and y
> and max3 has x and y and maybe other arguments to select the maximum from.
> The first "[]" in each fn are bindings for function arguments.
>
> Now you are allowed to define a fn with different forms based on the
> number of arguments:
>
> (defn max
> "Returns the greatest of the nums."
> {:added "1.0"}
> ([x] x) ;; <- max1
> ([x y] (if (> x y) x y)) ;; <- max2
> ([x y & more] (reduce max (max x y) more))) ;; <- max3
>
> is the equivalent of max1, max2 and max3 but using a single name.
> Clojure will select the form to execute based on the # of arguments.
> "x" is a different binding in each of the forms, not the same "x"
> across forms.
>
> Luc P.
--
Michael Wood <[email protected]>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en