Arc Forumnew | comments | leaders | submitlogin
Pattern Matching in Arc: Now has guards
7 points by almkglor 6119 days ago | 1 comment
This just pushed on nex-3's arc-wiki.git:

  (defpat *defpat-rpn
        " Example/testcase for defpat in using
          ,(x (test x)) guarded patterns "
        ;start with an empty stack
        (lst)
                (*defpat-rpn lst '())
        ;all done; return what's on the stack
        (()                             (x))
                x
        ;extra stuff on the stack
        (()                             (_ . __))
                (ero "extra stuff on the stack")
        ;divide by zero
        ((,(f (is f /)) . _)            (0 x . __))
                (ero "divide by zero")
        ;pop, pop, push...
        ((,(f (isa f 'fn)) . xs)        (b a . s))
                (*defpat-rpn xs `(,(f a b) ,@s) )
        ;not enough to pop
        ((,(f (isa f 'fn)) . xs)        x)
                (ero "too few parameters")
        ;just push
        ((x . xs)                       s)
                (*defpat-rpn xs `(,x ,@s)))
edit: oh and by the way, patterns like this will work:

  (defpat foo
    ((x . 'x))   (prn "you have an x of "x)
    ((y . ,(s (isa s 'sym))))
        (prn "Not an x, but a " s " of " y))
To support the above, though, you can't use quote and unquote as variable names.


3 points by dreeves 6119 days ago | link

Thanks!

(For some background on the rpn example, see http://arclanguage.org/item?id=2115 )

I feel like some more syntactic sugar somehow could make the patterns in defpat more palatable.

-----