Arc Forumnew | comments | leaders | submitlogin
2 points by almkglor 6159 days ago | link | parent

Using my defpat macro (pushed on nex-3's arc-wiki.git):

  (defpat rpn
    (lst)          (rpn lst ())
    (() (x . xs))  x
    ((x . xs) (a b . s))
                   (if (isa x 'fn)
                      (rpn xs (cons (x a b) s))
                      (rpn xs `(,x ,a ,b ,@s)) )
    ((x . xs) s)
                   (if (isa x 'fn)
                      (ero "Too few parameters")
                      (rpn xs `(,x ,@s))))
Admittedly my defpat can't do the a_Symbol check, but then I can destructure more complex lists.


1 point by dreeves 6159 days ago | link

I like defpat. It looks soegaard's scheme solution does allow things like the a_Symbol check, right? Also, mathematica does allow destructuring arbitrarily complex lists:

  f[{{a_,{b_,c_,{{d_},e_}}}}] := g[a,b,c,d,e]

-----

1 point by almkglor 6159 days ago | link

The problem in a lisplike is how to properly embed checks in destructuring binds. Since code represenetation == data representation, it's rather difficult to do:

  (defpat foo
    (x (isa x 'sym))
        (list x isa))
As it is, I'm already forbidding one symbol from being used for variables: quote. This is so that I can support:

  (defpat cmd
    ('niaw)   "cat"
    ('arf)    "dog"
    ('squeek) "mouse"
    (x)       (ero "unknown sound!"))

I don't know how scheme's pattern matching works; possibly yet another symbol has been removed from the allowed symbols.

-----

1 point by almkglor 6159 days ago | link

I've since added guards in defpat. See the arc-wiki, also http://arclanguage.org/item?id=2276

-----