Re: How does one define a closure in m4?

Doug McIlroy <[email protected]>
Newsgroups gmane.comp.gnu.m4.general
Message-ID <[email protected]>
[email protected] asked how to do a closure by having one macro
define another. The difficulty is in distinguishing which
macro a parameter belongs to--the inner or the outer. 
The solution is to hide parameter designations for the
iinner macro so they appear only upon expansion of the
outer macro. The following classic example illustrates
one way to do this.

specialize(unary,binary,value) produces a unary operation
by fixing the value of one the operands of a binary operation.

define(add,`($1+$2)')
define(div,`($1/$2)')
define(specialize,`define($1,`$2('$`1,$3)')')
specialize(succ,`add',1)
specialize(half,`div',2)

With these definitions, half(succ(4)) expands to ((4+1)/2)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.