On 19 July 2010 19:41, Laurent PETIT <laurent.pe...@gmail.com> wrote: > 2010/7/19 Stuart Halloway <stuart.hallo...@gmail.com> >> >> "use" = "rely on" >> >> In your example, func relies on a variable which is (presumably, based on >> its use in other-func) intended for dynamic binding. Therefore, func is >> impure. >> >> It is idiomatic to name such variables with earmuffs, e.g. *forty-two*. >> > > Sure. > But the OP is somehow true: it is "by convention" that it is declared pure. > Indeed, even func itself could have been rebound dynamically, thus making > the content of the result "theoretically unpredictable", given only the > values of its arguments.
I think the point being made here is that, because other-func uses dynamic binding (and hence is "impure") you have to be more careful about how you code it. For example, in the absence of "impure" code, it is perfectly acceptable to name intermediate values freely - so (defn demo1 [] (+ (some-fn) 17)) and (defn demo2 [] (let [res (some-fn)] (+ res 17))) are exactly the same. That doesn't work with the impure other-func: (defn other-func [] (binding [forty-two 6] (func))) vs (defn other-func-2 [] (let [res (func)] (binding [forty-two 6] res))) Transformations like naming intermediate values only work (at least, without analysis) in the definition of pure functions. When coming from an imperative language like Java or C, this seems glaringly obvious - "of course" you have to be careful when moving code round like that. The point is that in functional languages (and in Clojure when you avoid "impure" functions) you *don't* have to be careful - you can do as much of that type of refactoring as you want, without worrying. I hope that helps clarify things. Paul. -- 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 Note that posts from new members are moderated - please be patient with your first post. 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