Hi Sam,
On Tue, Aug 9, 2011 at 3:50 PM, Sam Aaron <[email protected]> wrote:
>
>
> Do you happen to have any simple descriptions/examples of where and how we
> might use this stuff in our daily programming repertoires?
Think of pattern matching sugar for nested conditionals.
For example
(match [x y]
[1 0] 0
[0 1] 1)
expands to:
(cond
(= x 0) (cond
(= y 1) 1
:else (throw (java.lang.Exception. "Found FailNode")))
(= x 1) (cond
(= y 0) 0
:else (throw (java.lang.Exception. "Found FailNode")))
:else (throw (java.lang.Exception. "Found FailNode")))
Here's a handy macro to see what's going on under the covers:
match.core=> (defmacro expand-pattern [vars & clauses]
`(-> (build-matrix ~vars ~@clauses)
compile
to-clj
source-pprint))
#'match.core/expand-pattern
match.core=> (expand-pattern [x y]
[1 0] 0
[0 1] 1)
(cond
(= x 0) (cond
(= y 1) 1
:else (throw (java.lang.Exception. "Found FailNode")))
(= x 1) (cond
(= y 0) 0
:else (throw (java.lang.Exception. "Found FailNode")))
:else (throw (java.lang.Exception. "Found FailNode")))
nil
match.core=>
Thanks,
Ambrose
--
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