Arc Forumnew | comments | leaders | submitlogin
1 point by dreeves 6158 days ago | link | parent

Thanks almkglor and sacado! At first I thought I was soundly defeated in the pattern-matching args challenge but I notice you don't catch the errors, either division by zero or too much stuff on the stack at the end. The fact that I can stick that in to my version without touching the rest of my code I feel points to the advantage of this style of programming.

And note that if I remove those error checks, my code is this:

  rpn[lst_] := rpn[lst, {}]
  rpn[{}, {x_,___}] := x
  rpn[{a_Symbol, b___}, {x_, y_, r___}] := rpn[{b}, {a[y,x], r}]
  rpn[{a_, b___}, {s___}] := rpn[{b}, {a,s}]


2 points by almkglor 6158 days ago | link

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 6158 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 6158 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 6158 days ago | link

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

-----