On Fri, Apr 16, 2010 at 2:29 PM, Bytesource <[email protected]> wrote:
> Hi,
>
> I am currently reading "Programming Clojure" but got stuck at the
> destructuring done in the "head-overlaps-body?" function call that is
> part of the "snake" game:
>
> (defn head-overlaps-body? [{[head & body] :body}]
> (includes? body head))
>
> ;; page 200 of the pdf version
>
> I can not figure out what {[head & body] :body} actually means here.
The :body on the map returns something like this:
[[1 1] [1 2] [1 3] [1 4] [1 5]]
i.e. each co-ordinate is a vector and the whole set of vectors are
inside another vector. By the destructuring binding {[head & body]
:body}, you are separating out the head and the rest of the body.
(def snake-body {:body [[1 1] [1 2] [1 3]]})
(:body snake-body)
=> [[1 1] [1 2] [1 3]]
(let [[head & body] (:body snake-body)]
head)
=> [1 1]
(let [[head & body] (:body snake-body)]
body)
=> ([1 2] [1 3])
Hope this helps.
--
Ramakrishnan
--
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