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

The do is there because if takes the form (if pred then else), and in
this case the "then" is supposed to be two statements executed for
side-effects.

Removing the do form would actually result in something different than
above; what you've done is replaced a (do form1 form2) with a ((form1)
form2). In other words, you're calling (ref-set apple (create-apple)),
and then *calling the return value of that* with the parameter (alter
snake move :grow).

Why this seems to work I don't know without looking at the snake
example, but presumably (create-apple) creates an apple, and
presumably an "apple" is something which is callable (probably a map,
so that when called with the return-value of (alter snake move) it
simply returns nil or some value associated with that key).

-- 
/ Peter Schuller

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
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

Reply via email to