On Tue, Nov 3, 2009 at 4:22 PM, Brian Hurt <[email protected]> wrote:

>
>
> On Tue, Nov 3, 2009 at 4:21 PM, Dean Wampler <[email protected]>wrote:
>
>> Ah, of course. Thanks. This works:
>>
>> (defn for-each [f items]
>>   (if (not (empty? items))
>>       (let [] (f (first items)) (for-each f (rest items)))))
>>
>>
> Or:
>
> (defn for-each [ f items]
>
>
Argh.  Hit send when I didn't mean to.  Sorry.  Try again:
(defn for-each [f items]
    (if (not (empty? items))
        (do
            (f (first items))
            (for-each f (rest items)))))

Of course, you don't have tail call optimization in Clojure (dang jvm).  So
this is probably better:
(defn for-each [f items]
    (loop [ curr items ]
        (if (empty? curr)
            nil
            (do
                (f (first curr))
                (recur (rest curr))))))

Brian

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