m4-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How does one define a closure in m4?


From: Doug McIlroy
Subject: Re: How does one define a closure in m4?
Date: Sat, 07 Sep 2013 16:39:15 -0400
User-agent: Heirloom mailx 12.5 7/5/10

address@hidden 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)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]