Re: how to emulate lisp's labels functionality?
On 15.02.2009, at 06:26, Chouser wrote: On Sat, Feb 14, 2009 at 11:32 PM, GS gsincl...@gmail.com wrote: On Feb 14, 12:21 pm, Chouser chou...@gmail.com wrote: (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))) Yes, you're absolutely right. however, the recur isn't in tail position, so it doesn't work: user= (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))) java.lang.UnsupportedOperationException: Can only recur from tail position (NO_SOURCE_FILE:12) all the best, --Chris --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
how to emulate lisp's labels functionality?
Hi, do we have labels equiv. in clojure? The code below is from OnLisp. Trying to convert to clj file, but have minor difficulties. (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))) (count-instances ’a ’((a b c) (d a r p a) (d a r) (a a))) (1 2 1 2) thanks sun --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: how to emulate lisp's labels functionality?
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 -~--~~~~--~~--~--~---
Re: how to emulate lisp's labels functionality?
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 -~--~~~~--~~--~--~---