Also how left and right gets mapped magically to (first tree) and
(rest tree)?
Is it destructuring? I have some ideas on destructuring but this left
and right things confuses me...

thanks
sun


On Feb 15, 11:31 am, wubbie <sunj...@gmail.com> wrote:
> thanks,
> it works, although it does not count nil node.
> Anyway, what's the motivation behind using "sequential?" ?
> thanks
> sun
>
> On Feb 15, 11:23 am, Vincent Foley <vfo...@gmail.com> wrote:
>
> > Sorry about the erroneous function, I think this is more likely what
> > you want:
>
> > (defn count-leaves [[left & right :as tree]]
> >   (if (seq tree)
> >     (+ (if (sequential? left)
> >          (count-leaves left)
> >          1)
> >        (count-leaves right))
> >     0))
>
> > user> (count-leaves [])
> > 0
> > user> (count-leaves [1 [2 [3]]])
> > 3
> > user>
>
> > On Feb 15, 10:55 am, wubbie <sunj...@gmail.com> wrote:
>
> > > Hi,
>
> > > Trying to convert to clojure but stumbled on it...
>
> > > (defun count-leaves (tree)
> > >   (if (atom tree)
> > >   1
> > >   (+ (count-leaves (car tree))
> > >     (or (if (cdr tree) (count-leaves (cdr tree)))
> > >         1))))
>
> > > > (count-leaves ’((a b (c d)) (e) f))
>
> > > 10
>
> > > My clojure version is:
>
> > > user=> (defn count-leaves
> > >   [tree]
> > >    (if (seq tree)
> > >      (+ (count-leaves (first tree))
> > >         (or (if (rest tree) (count-leaves (rest tree)))
> > >             1))
> > >      1))
> > > #'user/count-leaves
> > > user=> (count-leaves [1 2 3])
> > > java.lang.IllegalArgumentException: Don't know how to create ISeq
> > > from: Integer (NO_SOURCE_FILE:0)
>
> > > But not working. I'm wondering why...
>
> > > thanks,
> > > -sun
>
>
--~--~---------~--~----~------------~-------~--~----~
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
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