4 ms·
For the non-lisp programmer, can someone explain how the Macro differs from a library call, or other function? If you're building the language, vs building your
by b_emery 5y ago
For the non-lisp programmer, can someone explain how the Macro differs from a library call, or other function? If you're building the language, vs building your code, is this really that different?
Having read about this, the best analogy I can think of is that macros are like engineered wood. If you're building a structure, you can build bigger ones with engineered wood, than with run of the mill (literally) stuff.
- AlexCoventry 5y agoMacros let you change the syntax, roughly speaking. With a library, you're still calling the library with standard syntax. E.g. https://en.m.wikipedia.org/wiki/Anaphoric_macro https://en.m.wikipedia.org/wiki/Anaphoric_macro
- thibran 5y agoA macro is a bit like a template in web programming. You define a structure and some variables that will be filled in. The difference to a template is, a template does not respect context, a macro does. For example, let's say your language had 'if' but not 'case', then it would be possible to write a case macro that in the end transforms at compile time into a bunch of ifs. Macros allows you to create new language syntax and express your intent more clearly.
- Abhinav2000 5y agoMacros let you construct expressions at compile time (not run time). So one can basically create their own programming language in macros, which at compile time expands into the standard language (e.g. lisp). In this way they are powerful for being expressive and also for being highly efficient (as its compile time, not run time). There are many other nifty things you can do as a result, but I am myself not that great. Paul Graham actually wrote the book on macros ('On Lisp'), he's legit :-)