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.
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.
On Sun, 10 Jul 2011 16:51:32 -0700 (PDT)
octopusgrabbus <[email protected]> wrote:
> If the function is called with one argument, what Clojure language
> rule allows x to appear outside the vector brackets?
>
> On Jul 10, 7:18 pm, Jonathan Fischer Friberg <[email protected]>
> wrote:
> > There's no interfaces, that's the function definition.
> >
> > define function max
> > (defn max
> >
> > attach docstring
> > "Returns the greatest of the nums."
> >
> > attach metadata
> > {:added "1.0"}
> >
> > if max is called with one argument, use this function definition
> > ([x] x)
> >
> > if max is called with two arguments, use this function definition
> > ([x y] (if (> x y) x y))
> >
> > if max is called with more than two arguments, use this function
> > definition ([x y & more]
> > (reduce max (max x y) more)))
> > ___________
> >
> > As you can see, y is introduced in one of the functions definitions.
> > Also
> > see:http://clojure.org/special_forms#Special%20Forms--(fn%20name?%20[params*%20]%20exprs*)<http://clojure.org/special_forms#Special%20Forms--%28fn%20name?%20[params*%20]%20exprs*%29>
> > andhttp://clojure.org/metadata
> >
> > Jonathan
> >
> > On Sun, Jul 10, 2011 at 11:44 PM, octopusgrabbus
> > <[email protected]>wrote:
> >
> > > For Question 1 this is an example of multiple interfaces. Got it.
> >
> > > On Jul 10, 5:42 pm, octopusgrabbus <[email protected]>
> > > wrote:
> > > > From Clojure api for max
> >
> > > > (defn max
> > > > "Returns the greatest of the nums."
> > > > {:added "1.0"}
> > > > ([x] x)
> > > > ([x y] (if (> x y) x y))
> > > > ([x y & more]
> > > > (reduce max (max x y) more)))
> >
> > > > Question 1: Why can y be introduced as a local binding without
> > > > a let?
> >
> > > > Question 2: What is the map {:added "1.0"} doing?
> >
> > > > Thanks.
> > > > cmn
> >
> > > --
> > > 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
>
--
Luc P.
================
The rabid Muppet
--
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