I didn't expect my question would initiate such a wonderful
discussion, I'm speechless.
Thank you all guys, you are amazing.
Alex, your posts killed in a very good way :)
It was really helpful to morph the code and transform it.

On Dec 6, 11:14 am, Ken Wesson <kwess...@gmail.com> wrote:
> On Mon, Dec 6, 2010 at 4:02 AM, nickik <nick...@gmail.com> wrote:
> > The easy (and good) solution is to pass the running result onlong the
> > way. This can be done in diffrent ways.
>
> > First the CL Style where you creat a new function in you function that
> > then does all the work. (from
> > Ken Wesson)
>
> > (defn list-length [coll]
> >  (let [ll (fn [n s]
> >             (if s
> >               (recur (inc n) (next s))
> >               n))]
> >    (ll 0 (seq coll))))
>
> > I don't really like this because the core logic is somwhat hidden.
>
> ???
>
> > Then we could use arity functions. (Alex Osborne)
>
> >    (defn list-length
> >      ([coll]   (list-length coll 0))
> >      ([coll n] (if-let [s (seq coll)]
> >                     (recur (rest s) (inc n))
> >                      n)))
>
> > This is nice style in my opinion
>
> Not in mine -- it exposes a public API of
> list-length-plus-some-integer that a) doesn't make much sense and b)
> is unlikely to be supported, since the implementation could be changed
> to one that doesn't expose this. Of course sometimes a user wants to
> add something to a length of a seq and then they could get a slight
> speedup by using (list-length s n) instead of (+ n (list-length s)),
> but that is c) premature optimization in the typical case.
>
>
>
>
>
> > but I would do it like this:
>
> > (defn list-lenght [coll]
> >   (loop [coll coll n 0]
> >      (if (empty? coll)
> >          n
> >          (recur (rest coll) (inc n)))))
>
> > or with seq instead of empty?
>
> > (defn list-lenght [coll]
> >   (loop [coll coll n 0]
> >      (if (seq coll)
> >          (recur (rest coll) (inc n))
> >          n)))
>
> > Hope that helps a little.
>
> Why not
>
> (defn list-length [coll]
>   (let [s (seq coll)]
>     (loop [s s n 0]
>        (if s
>          (recur (next s) (inc n))
>          n))))
>
> ? This may be slightly more efficient at no cost in clarity.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to