Adding to what Meikel said: A warning sign that the latter version is incorrect is the double open parens: "((ref-set ..." Double open parens are fairly rare in Clojure code.
Stu > Hi, > > On Aug 5, 4:18 pm, michele <michelemen...@gmail.com> wrote: >> ORIGINAL >> >> (defn update-positions [snake apple] >> (dosync >> (if (eats? @snake @apple) >> (do (ref-set apple (create-apple)) >> (alter snake move :grow)) >> (alter snake move))) >> nil) >> >> WITHOUT do >> >> (defn update-positions [snake apple] >> (dosync >> (if (eats? @snake @apple) >> ((ref-set apple (create-apple)) <------ Removed "do" from >> here >> (alter snake move :grow)) >> (alter snake move))) >> nil) >> >> Both versions work, so why does the "do" on line 4 contribute? > > It is an accident that the second version works. Probably create-apple > returns a map. Replacing (create-apple) with - say - 5, should trigger > an exception. > > The do is required, because you can have only one expression for the > then-branch or the else-branch in an if. If you want to have several > expressions in a branch you need a do, which executes each expression > in a row and returns the value of the last one. So this is mostly > useful for side effects. > > In the example the side effects are the calls to ref-set and alter. > > Hope this helps. > > Sincerely > Meikel > > -- > 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 -- 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