What is the difference between rest and next? I'm confused, should I use empty? or not? when to use it?
Robert, Your code is working but if I use empty? , it returns 0 instead of the actual count. Why? Why Clojure decided to handle an empty list as a not false? this is a big (if not) departure from Common Lisp? Thank you guys for your help and time. On Dec 6, 12:16 am, Robert McIntyre <r...@mit.edu> wrote: > 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 <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? > > > -- > > 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 -- 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