> Do you happen to have any simple descriptions/examples of where and how we 
> might use this stuff in our daily programming repertoires? I for one am sure 
> I'm not educated enough as to the value and utility of pattern matching - at 
> the moment I just think "that looks cool" rather than "I'm definitely going 
> to use that to do X and Y".

To me pattern matching is the bee's knees :) If you have a lot of
functions where you check the input for some certain type of value and
then perform some action depending on the value(s), then pattern
matching can be very useful. It's used extensively in Erlang and
Haskell among others.

For example, say you have a function `decide` -

(defn decide
  "A very simple example using cond"
  [x y]
  (cond
   (= x :foo) (do-foo y)
   (= x :bar) (do-bar y)
   :else (do-default y)))

I expect two arguments x & y and depending on the value of `x`, I
would like to call the functions `do-foo`, `do-bar` or `do-default`
with the value of `y`.

See how the code is riddled with the conditional checks and my
business logic (the part I care the most about). Using
pattern-matching, I can write the above code a bit more succinctly
like this -

(defn decide
  "A very simple example using match"
  [x y]
  (match [x y]
    [:foo arg] (do-foo arg)
    [:bar arg] (do-bar arg)
    [_ arg] (do-default arg)))

The above code is *much* clearer and conveys the exact logic that I am
trying to implement. Internally `match` will rewrite the above to an
optimised bit of code which uses `cond`.

If I am feeling adventurous, I can write a simple macro which will let
me define the same function in an even more succinct manner like this
-

(defm decide
  "A very simple example"
  ([:foo arg]
    (do-foo arg))
  ([:bar arg]
    (do-bar arg))
  ([_ arg]
    (do-default arg)))

A sample implementation of the `defm` macro used above could be
something like this (UNTESTED!) -

(defmacro defm
  [name & fdecl]
  (let [m (if (string? (first fdecl))
            {:doc (first fdecl)}
            {})
        fdecl (if (string? (first fdecl))
                (next fdecl)
                fdecl)
        argcount (count (ffirst fdecl))
        args (vec (repeatedly argcount (partial gensym "arg__")))
        body (apply concat
                    (for [decl fdecl]
                      (if (nnext decl)
                        (cons (first decl) (list (cons `do (next decl))))
                        decl)))]
    `(defn ~name
       ~m
       ~args
       (match ~args
              ~@body))))

It's even more awesome when you bring in complex maps, etc. into the picture.

I personally believe that David and Ambrose are doing some incredible
work with match & core.logic; these two projects will prove be
extremely useful in the future.

Regards,
BG

-- 
Baishampayan Ghose
b.ghose at gmail.com

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