Arc Forumnew | comments | leaders | submitlogin
Conditional Macro for redundant branches
3 points by rincewind 5870 days ago | 3 comments
This macro lets implements a conditional form with braches that differ in some subexpressions, but have a common structure. It is supposed to make programs shorter by eliminating redundancy.

  (def improper (l)
     (if acons.l (improper cdr.l)
         l))

  (def sexp-trans (test fun tree)
     (if (test tree) (fun tree)
         (improper tree) tree
         (map [sexp-trans test fun _] tree)))

  (mac cond-alt (conds . code)
     (let test (andf acons [is 'alt car._])
        `(if ,@(apply join (rev:accum acc
           (forlen index conds
              (acc `(,conds.index (do ,@(sexp-trans test [cdr._ index] code))))))))))
Example:

  (let there 'ham
     (cond-alt ((is there 'spam) (is there 'ham) (is there 'bacon))
        (pr "there is " (alt 'spam 'ham 'bacon) 
            ", but there is no " (alt 'ham 'bacon 'spam)
            " or "(alt 'bacon 'spam 'ham) " in 'there.")))


2 points by andreyf 5869 days ago | link

I get - Error: "reference to undefined identifier: _M"

Is it me?

-----

3 points by cchooper 5868 days ago | link

Are you copy and pasting directly from the browser? If so, you might be getting weird line endings that Scheme doesn't understand. Try copying it into a text file first and then copying from there into Arc.

-----

2 points by andreyf 5868 days ago | link

That was it! So much headache over figuring that out, thanks :)

-----