How Lisp macros differ from static code-generation and metaprogramming
1–10 of 84 posts
Re: How Lisp macros differ from static code-generation and metaprogramming
#2#include
#define LOOP(n) \ int i; \ for (i = 0; i #define LOOP10 \ LOOP(10) printf("%d\n", i)
int main(int argc, char *argv[]) { LOOP10; }
Re: How Lisp macros differ from static code-generation and metaprogramming
#3For example:
(defun sum (x y)
`(,x + ,y = ,(+ x y)))
This is a SUM function which looks like this when run: (sum 4 5) ==> (4 + 5 = 9)
The backquote (`) says the following form and its sub-forms (if any) are all quoted. But you can poke holes in the quote and fill in with the value of outside variables, using comma (,). So in the example, everything is quoted in the body of sum, except three things: x, y, and (+ x y). Each of those is then evaluated yielding 4, 5 and 9, then, finally, the template is filled in (4 + 5 = 9).If you want to confirm this, you can change the + and = to any other symbol and it would still display. E.g:
(defun sum (x y)
`(,x frob ,y != ,(+ x y)))
(sum 4 5) ==> (4 FROB 5 != 9)Re: How Lisp macros differ from static code-generation and metaprogramming
#4If you've ever built a string in T-SQL or PL/SQL and then exec'd it, you're probably much closer to being productive with macros than you think.
Of course, this is probably because dynamically generating SQL is something that I've done. Other programmers probably have their own mental models that warp into lisp.
Re: How Lisp macros differ from static code-generation and metaprogramming
#5Re: How Lisp macros differ from static code-generation and metaprogramming
#61. Macros aren't unique to lisp. They're most developed and used in the lisp family, but you don't need to have a homoiconic language to have macros. The Mirah programming language--statically typed Ruby on the JVM--has non-hygenic macros, I know there's been a number of efforts to add macros to coffeescript (so far without success), and there are a number of academic languages that also have them. None are in widespread use, but they exist.
2. Metaprogramming's power is primarily limited by the restrictions of the host language. The best example I know of is Io where pretty much anything other than commas and parentheses is up for grabs. Example: http://anttih.com/blog/2010/10/29/json-in-io.html
3. User definable mixfix operators are in the sameish domain. Unfortunately, they're almost always mentioned in passing by people who know what they are so I don't have a good reference for introducing what they are and don't have direct experience working with them. I'm just mentioning them as something to be aware of if you're interested in general metaprogramming concepts. Here's the closest thing to an intro I could google up: http://maude.cs.uiuc.edu/maude1/manual/maude-manual-html/mau...
Re: How Lisp macros differ from static code-generation and metaprogramming
#7Re: How Lisp macros differ from static code-generation and metaprogramming
#8Re: How Lisp macros differ from static code-generation and metaprogramming
#9Nice try blanco niño, but the following program runs fine. This doesn't detract from your overall point, but if you're trying to win over people who think the Boost macros are super awesome, this argument won't do it. #include #define LOOP(n) \ int i; \ for (i = 0; i #define LOOP10 \ LOOP(10) printf("%d\n", i) int main(int argc, char *argv[]) { LOOP10; }
Here it is:
(defmacro for-duration ((seconds) &body body)
`(handler-case
(bt:with-timeout (,seconds)
,@body)
(bt:timeout () nil)))
Five lines to alter the evaluation model of your language. Not bad.Use as:
(for-duration (10)
(loop
(print "Infinite loop! where is your Godel now?")))
"bt" is the bordeaux-threads package, a portable Lisp library for thread programming.