On Apr 16, 11:59 am, Bytesource <stefan.rohlf...@gmail.com> 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.
>
> "head-overlaps-body?" is called inside "lose?" which is called with a
> "snake" object:
>
> (def lose? head-overlaps-body?)
>
> (defn create-snake []
>   {:body (list [1 1])
>    :dir [1 0]
>    :type :snake
>    :color (Color. 15 160 70)})
>
> I read the the information about the "let" binding form on clojure.org/
> special_forms but still have no clue how the above destructuring is
> done.
>
> Hope someone can give me a hint.
>

As it is a nested destructuring form, there are two kinds of
destructuring happening: First, there is map destructuring that takes
the value for the key :body from the argument map. That is, ([1 1]),
which is a list.
Normally the value would then be bound to a name on the left-hand
side, but in this case there's the destructuring form [head & body]
instead, which means sequential destructuring is applied to that list.
Thus, in this particular example, head gets bound to [1 1] and body to
nil.

--
Jarkko

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

Reply via email to