Mailing List Archive
tlug.jp Mailing List tlug archive tlug Mailing List Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]Re: [tlug] Pattern Matching
- Date: Wed, 4 Nov 2009 16:19:15 +0900
- From: Alan Busby <thebusby@example.com>
- Subject: Re: [tlug] Pattern Matching
- References: <a63167770911010442pd642beco3268ed5c4a41d02@example.com> <20091102115859.584591f3.attila@example.com> <a63167770911020238q2c68d37bt4efb354c91002470@example.com> <20091102112350.bf06bfd6.attila@example.com> <87hbtdtfch.fsf@example.com> <a63167770911020112q1bd12b27ub5209902a971a6c9@example.com> <20091102150646.GD16339@example.com> <a63167770911021615h14f8c2d4rf7ac6f7e3b44137a@example.com> <20091104013637.GF9636@example.com> <20091104014556.GA14456@example.com>
On Wed, Nov 4, 2009 at 10:45 AM, Curt Sampson <cjs@example.com> wrote:Actually, let me extend my pattern matching sample almost trivially by
adding another constructor to what used to be called Pair:
> data Maybe a = Nothing | Just a -- standard library definition
> data N = Singleton Int | Pair Int Int
>
> nonZeroValueOf :: Maybe N -> Maybe Int
> nonZeroValueOf x =
> case x of
> Nothing -> Nothing
> Just (Singleton 0) -> Nothing
> Just (Singleton n) -> Just n
> Just (Pair 0 0) -> Nothing
> Just (Pair 0 m) -> Just m
> Just (Pair n _) -> Just n
Sorry for the rush here, nor am I a Clojure master yet, but...
Illustrating the value of macros, it looks like some other people have been busy adding support for this to Clojure,
1. http://markmail.org/message/fsyxbt7iih3rp7cu
2. http://www.brool.com/index.php/pattern-matching-in-clojure
Using James Reeves implementation, I imagine it would look like this,
(defn nonZeroValueOf [x]
(match x
[nil] nil
[0] nil
[n] n
[0 0] nil
[0 m] m
[n m] n))
A quick test;
user> (nonZeroValueOf [nil])
nil
user> (nonZeroValueOf [0])
nil
user> (nonZeroValueOf [1])
1
user> (nonZeroValueOf [0 1])
1
user> (nonZeroValueOf [0 0])
nil
user> (nonZeroValueOf [2 0])
2
user> (nonZeroValueOf [2 3])
2
Here is the version of James Reeves implementation I'm using;
(defn has-match
[matches]
(if (some (partial = :no-match) matches)
:no-match
(apply concat matches)))
(def matches?)
(defn match-map
[x-map y-map]
(has-match
(map (fn [[k v]]
(if-let [y (y-map k)]
(matches? v y)
:no-match))
x-map)))
(defn match-seq
[xs ys]
(if (= (count xs) (count ys))
(has-match (map matches? xs ys))
:no-match))
(defn matches?
[x y]
(let [both #(and (% x) (% y))]
(cond
(= x `'~y) '()
(both seq?) (match-seq x y)
(both vector?) (match-seq x y)
(both map?) (match-map x y)
(symbol? x) (list x y)
(= x y) '()
true :no-match)))
(defmacro match [value & clauses]
(when clauses
`(let [~'derefs (matches? '~(first clauses) ~value)]
(if (not= ~'derefs :no-match)
(eval (list 'let (apply vector ~'derefs)
'~(second clauses)))
(match ~value ~@(nnext clauses))))))
- References:
- Re: [tlug] recomendations for a functional language
- From: Alan Busby
- Re: [tlug] recomendations for a functional language
- From: Attila Kinali
- Re: [tlug] recomendations for a functional language
- From: Alan Busby
- Re: [tlug] recomendations for a functional language
- From: Attila Kinali
- Re: [tlug] recomendations for a functional language
- From: Stephen J. Turnbull
- Re: [tlug] recomendations for a functional language
- From: Alan Busby
- Re: [tlug] recomendations for a functional language
- From: Curt Sampson
- Re: [tlug] recomendations for a functional language
- From: Alan Busby
- Re: [tlug] recomendations for a functional language
- From: Curt Sampson
- [tlug] Pattern Matching
- From: Curt Sampson
Home | Main Index | Thread Index
- Prev by Date: Re: [tlug] recomendations for a functional language
- Next by Date: Re: [tlug] recomendations for a functional language
- Previous by thread: [tlug] Pattern Matching
- Next by thread: Re: [tlug] recomendations for a functional language
- Index(es):
Home Page Mailing List Linux and Japan TLUG Members Links