Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the practical differences between special forms and macros?

Are there any practical differences between special forms and macros? In what do they differ?

like image 721
Pedro Rolo Avatar asked Feb 22 '12 14:02

Pedro Rolo


People also ask

What is a special form in programming?

A special form is a primitive function specially marked so that its arguments are not all evaluated. Most special forms define control structures or perform variable bindings—things which functions cannot do. Each special form has its own rules for which arguments are evaluated and which are used without evaluation.

Why do we need special forms in scheme?

Special Forms [1] Usually, an expression represents a procedure invocation, so the general rule is that Scheme first evaluates all the subexpressions, and then applies the resulting procedure to the resulting argument values. The specialness of special forms is that Scheme doesn't evaluate all the subexpressions.


1 Answers

The terms aren't quite synonymous, but they aren't exclusive either (this answer assumes Scheme):

  • A special form (also known as a syntax in the Scheme Reports) is an expression that's not evaluated according to the default rule for function application. (The default rule, just to be explicit, is to eval all of the subexpressions, and then apply the result of the first one to the list of the results of the others.)
  • The macro system is a language feature that allows definition of new special forms within the language itself. A macro is a special form defined using the macro system.

So you could say that "special form" is a term that pertains to interface or semantics, whereas "macro" is a term that pertains to implementation. "Special form" means "these expressions are evaluated with a special rule," while "macro" means "here's an implementation of a special rule for evaluating some expressions."

Now one important thing is that most Scheme special forms can be defined as macros from a really small core of primitives: lambda, if and macros. A minimal Scheme implementation that provides only these can still implement the rest as macros; recent Scheme Reports have made that distinction by referring to such special forms as "library syntax" that can be defined in terms of macros. In practice, however, practical Scheme systems often implement a richer set of forms as primitives.

Semantically speaking, the only thing that matters about an expression is what rule is used to evaluate it, not how that rule is implemented. So in that sense, it's not important whether a special form is implemented as a macro or a primitive. But on the other hand, the implementation details of a Scheme system often "leak," so you may find yourself caring about it...

like image 101
Luis Casillas Avatar answered Oct 13 '22 05:10

Luis Casillas