[Openmcl-devel] pattern matching

Taoufik Dachraoui dachraoui.taoufik at gmail.com
Wed Dec 8 03:22:42 PST 2010


Hi

I renamed the macro with-match-destruc to destructuring-match because
it is very close to the CL macro destructuring-bind with 2 differences:

1. addition of types
2. the macro returns nil if there is no match; this property
allow us to match a sequence against a list of pattern alternatives.

I created a macro cond-match that mimics the cond macro
and can be used as follows:

? (cond-match (2 3 4)
    ((a b (:type c 'symbol)) (list (+ a b) c))
    ((a b (:type c 'number)) (list (+ a b c))))
;Compiler warnings :
;   In an anonymous lambda form: Unused lexical variable #:G3852
;   In an anonymous lambda form: Unused lexical variable #:G3859
(9)

||#

(defmacro cond-match (seq &rest conds)
  (let ((x (gensym)))
    (cond
      ((null conds) nil)
      (t
       `(let ((,x (destructuring-match ,(caar conds) ',seq
    ,@(cdar conds))))
 (if (not (null ,x)) ,x
     (cond-match ,seq ,@(cdr conds))))))))

Kind regards
Taoufik


On Mon, Dec 6, 2010 at 11:38 PM, Daniel Weinreb <dlw at itasoftware.com> wrote:

>  Greg,
>
> This may be of no importance, but: unification is
> a very powerful mechanism.  If one is using
> cl-unification to do simple things like
> what defmacro does, is it reasonably fast?
>
> -- Dan
>
> Greg Pfeil wrote:
>
> On 4 Dec 2010, at 8:02, Taoufik Dachraoui wrote:
>
>
>
>  Hello
>
> I wrote a matching pattern function and would like to share in order
> to improve it (remove the compiler warnings for example, see below).
>
> I started with the destruc function found in onLisp book by Paul Graham.
>
>
>  I don't recall if it has any type restrictions, but I tend to use CL Unification (http://common-lisp.net/project/cl-unification/) for pattern matching stuff.
>
>
>
>  Any matching symbol x can be typed  and must be of the form (:type x #'name)
> where name is a boolean function with one argument (eg. numberp, consp, symbolp,
> atom, ...)
>
>
>  Why not have the form be (:type var typename), like (:type x '(integer 0 9))? Then you get much finer control without having to define unary functions for every type you care about. Should be able to just replace (funcall ,(third pat) (car ,seq)) with (typep (car ,seq) ,(third pat)) and you're golden.
> _______________________________________________
> Openmcl-devel mailing listOpenmcl-devel at clozure.comhttp://clozure.com/mailman/listinfo/openmcl-devel
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20101208/ce2b98c1/attachment.htm>


More information about the Openmcl-devel mailing list