Arc Forumnew | comments | leaders | submitlogin
Qi's Amazing Macros
3 points by Bogart 5989 days ago | 5 comments
There have been posts about the qi language before, but one thing that was always neglected was its cool macros. From the tutorial: The system function macroexpand is applied to all inputs typed to the top level and to every element of every file loaded into Qi. By default, macroexpand is defined as the identity function. However the macroexpand function can be redefined to allow the user to define her own shortcuts or notational styles. A simple macroexpansion can be written that enables arithmetic operations to be written in infix notation. (define macroexpand [X Arith Y] -> [Arith (macroexpand X) (macroexpand Y)] where (element? Arith [+ - * /]) [X | Y] -> (map macroexpand [X | Y]) X -> X) (tc +) (define factorial {number - -> number} 0 -> 1 X -> (X * (factorial (X - 1)))) factorial : (number - -> number)

In arc, to do something like this, you have use anarki's defcall. Could ark adopt a macro expansion feature like this?



8 points by almkglor 5989 days ago | link

  (load "foo.arc" your-macro-expander-here)
Anarki only

-----

5 points by rincewind 5988 days ago | link

load would be even more useful if you were able to supply your own reader as the second argument.

-----

4 points by almkglor 5988 days ago | link

  (def load-my-reader (file read)
    (push current-load-file* load-file-stack*)
    (= current-load-file* file)
    (after
      (w/infile f file
        (whilet e (read f)
          (eval (hook e))))
      (do (= current-load-file* (pop load-file-stack*)) nil)))

-----

1 point by Bogart 5988 days ago | link

Wouldn't this force macros and code to be in separate files, though?

-----

1 point by almkglor 5988 days ago | link

It forces macro-expanders and code to be in separate files. You can still put macros in the code file, as long as your macro-expander can understand them.

-----