On Dec 5, 9:52 pm, HB <hubaghd...@gmail.com> wrote:
> Hi,
> I'm trying to write a function that calculates the length of a list:
>
> (defn list-length [col]
>   (if col
>     (+ 1 (list-length(rest col)))
>     0))
>
> (list-length '(Java, Clojure, Scala))
>
> Upon running it in the REPL, I got the error:
> java.lang.StackOverflowError (test.clj:3)
>
> What is going wrong?

Unlike Common Lisp, the empty list is not a false value in Clojure, so
your condition is always true.

If you change your test to (if (empty? col) ... ), all should be good.

Cheers,
Danny.

-- 
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