Your function never actually ends  because even the empty list
evaluates to true :)

rlm.dna-melting> (if (rest '()) true false)
true

rlm.dna-melting> (if (next '()) true false)
false

so, changing your list length function to use next will work

rlm.dna-melting> (defn list-length [col] (if col (+ 1 (list-length
(next col))) 0))
#'rlm.dna-melting/list-length
rlm.dna-melting> (list-length (range 5))
5

hope that helps,
--Robert McIntyre

On Sun, Dec 5, 2010 at 4:52 PM, HB <[email protected]> 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?
>
> --
> 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

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

Reply via email to