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

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to