Live data from Hacker News

How Lisp macros differ from static code-generation and metaprogramming

brandonbyars.com

11–20 of 84 posts

Re: How Lisp macros differ from static code-generation and metaprogramming

#11
post #3

By the way, don't confuse macros with backquote; the backquote facility is probably closer to C's preprocessors. I haven't heard this claim before, and I might not be entirely right, but it's what provides for the "fill in the blanks" type symbolic computing. For 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…

You can also splice values into backquoted expressions using ,@foo rather than ,foo - which I seem to remember being rather useful.

Re: How Lisp macros differ from static code-generation and metaprogramming

#12
post #6

A couple points on the metaprogramming space (these aren't about the article, but the article reminded me of them): 1. 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 coffees…

1. Yet, still to this day, Lisp remains one of the few languages with powerful macros that normal people cannot only use, but enjoy. Also the Scheme/Racket community actively researches how to make them more robust and easier to write (yes equally powerful macro systems do exist for other languages - however, ease of use is often sacrificed)

2. Io is great but the depth of it's runtime meta-programming facilities make it horrifically inefficient.

Re: How Lisp macros differ from static code-generation and metaprogramming

#13
post #6

A couple points on the metaprogramming space (these aren't about the article, but the article reminded me of them): 1. 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 coffees…

1. Macros aren't unique to lisp.

Perl6 also has macros (http://en.wikipedia.org/wiki/Perl_6#Macros) though they haven't been implemented in Rakudo yet. Also Ioke states that it has macros.

2. Metaprogramming's power ... The best example I know of is Io where pretty much anything other than commas and parentheses...

Touched on Io introspection/metaprogramming before on HN:

* http://news.ycombinator.com/item?id=1804599

* http://news.ycombinator.com/item?id=1810480

Re: How Lisp macros differ from static code-generation and metaprogramming

#14
post #4

Lisp macros remind me of dynamically generating SQL statements. If 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.

Yeah, I think that's a fair way to start thinking about them. But Lisp macros, to extend your example, are more like if you could generate the SQL using SQL. Which sounds like a totally horrible thing actually. :) But with Lisp it makes much more sense because lisp programs are lists. If SQL programs were tables, then it'd be a better fit...

Re: How Lisp macros differ from static code-generation and metaprogramming

#15
post #4

Lisp macros remind me of dynamically generating SQL statements. If 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.

Yeah, I think that's a fair way to start thinking about them. But Lisp macros, to extend your example, are more like if you could generate the SQL using SQL. Which sounds like a totally horrible thing actually. :) But with Lisp it makes much more sense because lisp programs are lists. If SQL programs were tables, then it'd be a better fit...

You'd have to stretch the analogy to not only generating SQL with SQL, but extending SQL with SQL, to create constructs that weren't even possible to write in SQL before you extended it.

Re: How Lisp macros differ from static code-generation and metaprogramming

#16

Are natural languages homoiconic? (If you get my drift.) Does that question even make sense?

Hmm, I don't know about in general, but in English we have the double-quote when being used to refer to the statement itself. For example, I can talk about the use of "for example" by quoting it like that. So (written) English can embed English in a reasonably standard format. Very like the Lisp quote operator, no? The name is not coincidence.

However, we have no useful English operators to take the quoted words and change them into something else meaningfully. Though a non-English set can be argued to exist: http://creativeservices.iu.edu/resources/guide/marks.shtml If those were English themselves that would arguably complete the loop, but I would consider that stretching the point; these were explicitly invented to avoid the use of English and also translate to any other character-based language.

Re: How Lisp macros differ from static code-generation and metaprogramming

#17
> Except, of course, that doesn’t work. The preprocessor only makes one pass > through the file, meaning a macro can’t call another macro.

Huh? That's just blatantly wrong, and I (and many others) have used the C preprocessor to create multi-level macros in quite powerful ways. See for example http://blog.nelhage.com/2010/07/implementing-an-edsl-in-cpp/ or the insanity Linux's tracing macros have implemented: http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6...

His article has a point, but he's not doing C macros justice -- they are quite powerful and can be used in really cool ways, even if people tend to look at you funny when you do :)

Re: How Lisp macros differ from static code-generation and metaprogramming

#18
post #9
post #2

Nice 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; }

Yesterday I was writing code that kept messing my Lisp's signal handling, so I wrote a FOR-DURATION macro that arms a timeout and makes sure whatever that runs in its body gets killed after N seconds. 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-duratio…

    (defun for-duration (seconds body)
        (handler-case  
             (bt:with-timeout seconds 
        	   (body))
           (bt:timeout () nil)))
    
    (for-duration 10
        (lambda ()
          (loop 
            (print "Infinite loop! where is your Godel now?"))))

Re: How Lisp macros differ from static code-generation and metaprogramming

#19
post #6

A couple points on the metaprogramming space (these aren't about the article, but the article reminded me of them): 1. 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 coffees…

1. Macros aren't unique to lisp. Perl6 also has macros ( http://en.wikipedia.org/wiki/Perl_6#Macros ) though they haven't been implemented in Rakudo yet. Also Ioke states that it has macros. 2. Metaprogramming's power ... The best example I know of is Io where pretty much anything other than commas and parentheses... Touched on Io introspection/metaprogramming before on HN: * http://news.ycombinator.com/item?id=18045…

The question isn't so much whether other languages have something they call "macros", but whether their macro systems approach Lisp's in power, flexibility, ease of use, integration with the language, and natural fit on to the language representation? Or is the macro system in question more of a Turing tarpit?

Re: How Lisp macros differ from static code-generation and metaprogramming

#20
post #16

Are natural languages homoiconic? (If you get my drift.) Does that question even make sense?

Hmm, I don't know about in general, but in English we have the double-quote when being used to refer to the statement itself. For example, I can talk about the use of "for example" by quoting it like that. So (written) English can embed English in a reasonably standard format. Very like the Lisp quote operator, no? The name is not coincidence. However, we have no useful English operators to take the quoted words and…

I can't think of a natural language equivalent for the comma operator, other than (maybe) incredibly awkward and confusing uses of parenthetical statements
Post reply on HN