I'm not sure if it's "Clojury", but this seems to work:
(defn count-leaves [tree]
(if (sequential? tree)
(+ (count-leaves (first tree))
(count-leaves (rest tree)))
(or tree 0)))
user> (count-leaves [1 2 3])
6
user> (count-leaves [1 [2] 3])
6
user> (count-leaves [])
0
On Feb 15, 10:55 am, wubbie <[email protected]> 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 [email protected]
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
-~----------~----~----~----~------~----~------~--~---