On Feb 14, 12:21 pm, Chouser <[email protected]> wrote:
> > (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)))
Notwithstanding your more idiomatic implementation (snipped), wouldn't
"recur" be better than "thisfn"?
(defn count-instances [obj lsts]
(let [instances-in (fn [lst]
(if (seq lst)
(+ (if (= (first lst) obj) 1 0)
(recur (rest lst)))
0))]
(map instances-in lsts)))
Gavin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---