2 ms·
> How is a macro different than passing a quoted list to a function? Macro is a function that is executed in compile time. Think of it as a way of extending y
by sklogic 10y ago
> How is a macro different than passing a quoted list to a function?
Macro is a function that is executed in compile time. Think of it as a way of extending your compiler with new functionality. For example, your language does not have support for pattern matching originally. It's not a problem - you can define a macro that adds such a functionality to a language. You cannot do it with functions - it must be compiled and optimised before execution, and functions cannot introduce new identifiers into a scope.
Another simple example would be something like list comprehensions - also introducing variables and also requiring optimisations in compile time.
And at extreme level, macros can be used to turn your host language into something totally different. Not just add few new features, but turn it into another language. Lisp can become an ML or Haskell, or Prolog, or Java, or whatever else you can imagine. Macros can change syntax, can implement new semantics. None of it can be done with just functions.