Arc Forumnew | comments | leaders | submitlogin
3 points by pg 6160 days ago | link | parent

Do you have an example of a function you might write in an application, rather than the definition of one of the basic list traversal operators? As I said in the Arc Challenge, pattern-matching looks great when it's used for those, but tends not to be so useful in applications, precisely because operations like map, once they're defined, give you so much of what you need from pattern matching.


3 points by almkglor 6160 days ago | link

I wanted to use this as an example, but I suspect there's a bug when typ == 'choice - what's (cadr 'choice) ?

  (def varline (typ id val)
    (if (in typ 'users 'syms 'toks 'bigtoks)  (apply prs val)
        (is typ 'lines)                       (map prn val)
        (is typ 'yesno)                       (pr (if val 'yes 'no))
        (is typ 'choice)                      (varline (cadr typ) nil val)
        (text-type typ)                       (pr (or val ""))
                                              (pr val)))

-----

2 points by pg 6160 days ago | link

Oops, yes, you're right. I think that test should be (caris type 'choice).

-----

2 points by almkglor 6160 days ago | link

  (def varline (typ id val)
    (if (in typ 'users 'syms 'toks 'bigtoks)  (apply prs val)
        (is typ 'lines)                       (map prn val)
        (is typ 'yesno)                       (pr (if val 'yes 'no))
        (caris typ 'choice)                   (varline (cadr typ) nil val)
        (text-type typ)                       (pr (or val ""))
                                              (pr val)))

  (defpat varline-pat
    (,(typ (in typ 'users 'syms 'toks 'bigtoks))
        _ val)
         (apply prs val)
    ('lines _ val)
         (map prn val)
    ('yesno _ val)
         (pr (if val 'yes 'no))
    (('choice c) _ val)
         (varline c nil val)
    (,(typ (text-type typ)) _ val)
         (pr (or val ""))
    (_ __ val)
         (pr val))

-----

2 points by almkglor 6160 days ago | link

Might probably be shorter if I also add ,@(test _):

  (defpat varline-pat
    (,@(in _ 'users 'syms 'toks 'bigtoks)
        _ val)
         (apply prs val)
    ('lines _ val)
         (map prn val)
    ('yesno _ val)
         (pr (if val 'yes 'no))
    (('choice c) _ val)
         (varline c nil val)
    (,@(text-type _) _ val)
         (pr (or val ""))
    (_ __ val)
         (pr val))
The above won't work on nex-3's arc-wiki.git yet, but I'll add it this evening; I don't have access to my hacking computer right now.

-----

4 points by almkglor 6160 days ago | link

  (def eschtmlchar (c)
    (case c
       #\<  "&lt;"
       #\>  "&gt;"
       #\&  "&amp;"
            (string c)))

   (defpat eschtmlchar-pat
     (#\<)  "&lt;"
     (#\>)  "&gt;"
     (#\&)  "&amp;"
     (c)    (string c))

-----