On Fri, Jun 7, 2013 at 11:09 AM, larry google groups <
lawrencecloj...@gmail.com> wrote:

>
> I am very stupid and I am having trouble figuring out how to read
> this:
>
> (defmacro match-func [& body] `(fn [~'x] (match [~'x] ~@body)))
>
> ((match-func [q :guard even?] (+ 1 q) [z] (* 7 z)) 33)
> ;; 231
>
> What? Why 231?
>

Because 7 * 33 = 231.

The macro (which IMO is terrible and shouldn't be emulated) creates a
function of one argument that matches its argument with match/action
expressions given in its invocation. So

(match-func [q :guard even?] (+1 q) [z] (* 7 z))

expands into

(fn [x]
    (match [x]
       [q :guard even?] (+ 1 q)
       [z] (* 7 z)))

i.e., match the argument with q if it's even and return (+ 1 q); otherwise,
match it with z and return (* 7 z). Since 33 is odd, you get 231.

In general, using macroexpand-1 is very useful for figuring out what's
happening with a macro.



> The article is here:
>
> http://java.dzone.com/articles/my-first-clojure-macro
>
> I especially struggle with the longer version:
>
> (defmacro match-pfunc [& body]
> "Create a partial function that does pattern matching."
> (let [rewrite (mapcat (fn [x] [(first x) true]) (partition 2 body))]
> `(fn ([x#] (match [x#] ~@body))
> ([x# y#]
> (cond
> (= :defined? x#)
> (match [y#] ~@rewrite)
> (= :body x#)
> '(~@body))))))
>
>
> The article says:
>
> What this gives us is a function than can be invoked with a single
> parameter:
> (pf 44)
> And it can be invoked with 2 parameters:
> (pf :defined? 44)
>
>
> I keep looking at these 2 lines thinking maybe they reveal how
> different parameters cause a different action to be triggered, but I
> am unable to make sense of it:
>
> `(fn ([x#] (match [x#] ~@body))
> ([x# y#]
>
> The anonymous fn is simply being returned? Is it returned at compile
> time, or run time?
>
>
>
> --
> --
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>


-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to