Macro Questions

Started by Jeremy Dunn, January 11, 2005, 07:19:45 AM

Previous topic - Next topic

Jeremy Dunn

I'm trying to learn how to write macros but like so often in LISP some of the "simplest" things to learn are also the most complicated to get into ones head. So I figure if I study a few examples I might get the hang of it. With that in mind I would like to pose some syntax suggestions that would be useful as well as making good examples if anyone wants to tackle them.



1. A macro to convert a ! in front of a boolean function name into an enveloping 'not' expression. (!fn? a b ... n) becomes (not (fn? a b ... n)). I chose ! because it is used with the equal sign to denote negation as in = and != .



2. Nested expressions create lots of parentheses that would be nice to get rid of. How about a macro to convert (fn3 fn2 fn1 | a b ... n) into

(fn3 (fn2 (fn1 a b ... n))) where a vertical slash is used to separate the functions from the arguments.



3. The C-language has the operators += *= and so on. How about generically operating upon any function that takes two arguments by appending the function name with @? So we would have that (fn@ x y) becomes (setq x (fn x y))



4. Convert a boolean function into an if/then statement by having a double question mark on the end of the function name. For instance,

(if (= x 3) A B) becomes (=?? x 3 A B) where the last two arguments are always considered the 'then' and 'else' parts of the conditional. Of course we would also like to combine this with the ! to write

(if (!integer? x) A B) as (!integer?? x A B).

Sammo

#1
I believe you are talking about "reader macros" that operate on expressions as they are entered at the command line or via the 'load' function. Reader macros operate in the "read" part of Lisp's "read-execute-print" loop. Their job is to rewrite non-Lisp syntax into executable Lisp syntax. As far as I know, newLisp does not support reader macros. Seehttp://www.alh.net/newlisp/phpbb/viewtopic.php?p=84&highlight=reader+macro#84">this thread.

Jeremy Dunn

#2
That's too bad. Perhaps a compromise solution could be done for one of them by making a function out of it? For instance, could we write a function



(ifn funcname arg1 arg2 ... argN <then condition><else condition>)



that would takes its arguments and internally do



(if (funcname arg1 arg2 ... argN) <then condition><else condition>)