Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the advantages of scheme macros?

Why would anyone prefer Scheme macros over Common Lisp macros (and I genuinely want to know too, I'm not trying to be a troll)?

My experience as a Lisp newb is that Common Lisp style macros are much easier to learn than Scheme's macros. I have yet to see any advantages to Scheme's macros, but of course that doesn't mean they don't exist.

I do know that Scheme macros are "hygenic", but I'm still not convinced this is worth the additional complexity. On the other hand though, there obviously are people that are convinced that this is necessary, otherwise there wouldn't be implementations of Scheme macros in Common Lisp.

To make a long story short, can someone defend Scheme's macros to me?

like image 237
Jason Baker Avatar asked Aug 14 '10 16:08

Jason Baker


People also ask

What are macros in scheme?

A macro is a symbol that has a transformer procedure associated with it. When Scheme encounters a macro-expression — i.e., a form whose head is a macro —, it applies the macro's transformer to the subforms in the macro-expression, and evaluates the result of the transformation.

Why are Lisp macros useful?

The special power that Lisp macros have is that they can control evaluation (as seen by evaluating the input expression via ~expr and do arbitrary source-to-source transformations with the full power of the language available.


2 Answers

Scheme macros introduce two, essentially orthogonal, concepts: hygiene and pattern matching. Hygiene is less important in a lisp2 like Common Lisp. The pattern matching language captures many of the common macro idioms, but has the problem that it is essentially a different language from scheme. Probably the best introduction to scheme's macros, along with some of the rationale behind them is Shriram Krishnamurthi's PLAI chapters 36 and 37.

I suspect that the reason people write scheme style macro systems in common lisp is more for the pattern matching than for the hygiene.

like image 187
deinst Avatar answered Oct 05 '22 07:10

deinst


Because they use a different, non-Scheme language, Scheme macros are less powerful than Common Lisp macros in the almost-formal sense: you can do arbitrary compile-time computation with them, but it's hairy and convoluted. It's a lot like the argument for not using set!: less-powerful set!free languages produces less buggy code in exchange for awkward handling of state. Any time you trade power for discipline, you are betting that you will be able to build more complex systems in the long run.

That's the best argument I've seen for Scheme macros over Common Lisp ones: if you are building a complex language on top of Scheme, you are less likely to introduce subtle macro bugs if you stick with the standard macro system.

Personally, I don't build big languages using macros, so I prefer Common Lisp macros. I find them much easier for small jobs and avoiding variable capture etc isn't a big deal on a small scale.

like image 24
Nathan Shively-Sanders Avatar answered Oct 05 '22 06:10

Nathan Shively-Sanders