Ah that was what I was missing. You are right,
I misunderstood the recur form.

For some reason I thought in recur you have to specify the
name of the variable that it's bound to.

so when I wrote
(recur (newlist (cons (first x) newlist))

I thought I was saying in recur, rebind (cons (first x) newlist) to
newlist.

Thanks for the info!

On Mon, Nov 9, 2009 at 2:47 PM, Meikel Brandmeyer <m...@kotka.de> wrote:
> Hi,
>
> Am 09.11.2009 um 20:08 schrieb Wilson MacGyver:
>
>> (defn group [x]
>> (loop [newlist [] currlist x]
>>  (if (not (empty? x))
>>    (recur (newlist (cons (first x) newlist))
>>           (currlist (drop-while #(= (first currlist) %) currlist))
>>           ))))
>>
>>
>> It seems logical to me, but when I tried it with
>> (group [1 1 2 2 3 3 ])
>>
>> I get
>> CompilerException java.lang.IllegalArgumentException: Key must be
>> integer
>>
>> what am I missing?
>
> I think there is a misunderstanding how recur works. What you actually
> do, is to call newlist (a vector, acting as a function in this case).
> This is possible to retrieve the value at that index. ([1 2 3] 2) =>
> 3. However you pass a Cons. Hence the exception.
>
> A non-lazy implementation with loop/recur is surprisingly ugly. You
> need two different loops: one to traverse the input list, one to count
> the number of occurrences of an item.
>
> Sincerely
> Meikel
>
>



-- 
Omnem crede diem tibi diluxisse supremum.

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