On Fri, Feb 13, 2009 at 8:01 PM, Phil Hagelberg <p...@hagelb.org> wrote:
>
> wubbie <sunj...@gmail.com> writes:
>> Hi, do we have labels equiv. in clojure?
>> The code below is from OnLisp. Trying to convert to clj file,
>> but have minor difficulties.
>
> You can use "let" since variables and functions are kept in the same
> namespace.

This is true, but this...

> (defn count-instances [obj lsts]
>   (let [instances-in (fn [lst]
>                        (if (cons? lst)
>                          (+ (if (= (first lst) obj) 1 0)
>                             (instances-in (rest lst)))
>                          0))]
>   (map instances-in lsts)))

...doesn't work.  You can't refer to the 'let' local from within the
fn definition.  You can however give the fn a name, like thisfn:

(defn count-instances [obj lsts]
  (let [instances-in (fn thisfn [lst]
                       (if (seq lst)
                         (+ (if (= (first lst) obj) 1 0)
                            (thisfn (rest lst)))
                         0))]
        (map instances-in lsts)))

That works, but is not very Clojurey.  Just for the record, I might do
it more like:

(defn count-instances [obj lsts]
  (map #(count (filter #{obj} %)) lsts))

--Chouser

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