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.

> (defun count-instances (obj lsts)
>   (labels ((instances-in (lst)
>     (if (consp lst)
>       (+ (if (eq (car lst) obj) 1 0) (instances-in (cdr lst)))
>       0)))
>  (mapcar #’instances-in lsts)))

...becomes...

> (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)))

This is a pretty direct translation; you could make it more idiomatic in
other ways; I'm just trying to compare "let" to "labels".

-Phil

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