higher-order macro (foldoc) | higher-order macro
A means of expressing certain
higher-order functions in a first-order language, proposed
by Phil Wadler. Higher-order macros cannot be recursive
at the top level but they may contain recursive definitions.
For example, the normal, definition of the map function,
map f [] = []
map f (x:xs) = f x : map f xs
is higher-order because its argument, f, is a function. The
alternative formulation
map f l = map_f l
where
map_f [] = []
map_f (x:xs) = f x : m xs
defines a first-order function, map_f, that is a
specialisation of map in its first argument. This can be
considered a macro because it works purely by textual
substitution, requiring no knowledge about f for its validity.
This is an example of partial evaluation - the call, map f l,
has been partially evaluated to yeild an intermediate result.
This may be useful in optimising compilation or execution,
e.g. if the call to f can be subject to in-lining or when
executing map_f on a long list.
(2018-05-25)
|