m4-discuss
[Top][All Lists]
Advanced

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

functional programming (Was: Link time)


From: Eric Blake
Subject: functional programming (Was: Link time)
Date: Fri, 16 Jan 2009 07:08:28 -0700
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.19) Gecko/20081209 Thunderbird/2.0.0.19 Mnenhy/0.7.5.666

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

[Please keep the list in the loop]

According to address@hidden on 1/15/2009 7:53 PM:
> Many thanks, Eric Blake.
> The m4.texinfo document direct from git repository , specifically the
> section
> building macros with macros was very helpful, to say the least.
> Another plus in the lesson was how you used the name `define_blind' : )
> Also useful is how you described that by using recursion where iteration
> could
> have been possible avoids additional ifelse over-head.

I've learned a lot as the autoconf m4sugar maintainer, and have tried to
update the m4 manual accordingly.

> The effect achieved in the Japanese translated website was the inclusion of
> /dev/fd/0 to be piped through the use of m4 by the shell in solving some
> problem.
> Call-by-passing occurs therefore the use of inner-quoting to delay expansion
> where the argument is passed for calling by a nested define(eg., $`'#
> for nargs)
> in case the macro is a token already defined, or case we want a literal
> $#(to
> try and stay relevant) for the `helper' macro.

Cute trick, but platform-dependent (not all platforms have /dev/fd/0).

> A clever person with the website mbreen.com has posted at mbreen.com/m4.html
> an article describing uses for m4 that require thought. On the frontier
> there is
> use of defn to simulate arrays. Briefly he discusses macros to build macros.

I've seen that page before.  His suggested substr rewrite that parses a
string one letter at a time is just as prone to failure as the recursive
length macro on the uszla site.  Really, the only way to make substr
safely return quoted strings is to add the qindir builtin (planned for m4
1.6), and even then, you have to be careful of not splicing out unbalanced
quotes as part of your substring.  As for the notion of arrays and
associative arrays, the m4 manual already has an example of those.

> I have some fun ideas to try starting with currying. Is it possible to
> simulate Polytypic
> functions to be used similarly like in the example:
> http://www.cse.chalmers.se/~patrikj/poly/#polyfuns
> ie., the construction of algorithmic functions, operations on members of
> a structure/array
> of the same psuedo-type by means of the type of operation done on
> them(how it is operated on)?

Probably possible, since m4 is Turing complete, but I'm not sure how much
effort it will take.

Meanwhile, I've been experimenting with the idea of documenting anonymous
functions in the manual.  What do you think of this exposition?

M4 macros must always be named.  However, there is a way to create the
illusion of anonymous macros, known in computer science as the lambda
construct.  The trick is to define a temporary macro whose name goes out
of scope before any arguments are collected, so that the overall namespace
is unaffected by the macro definition.

define(`lambda',
`pushdef(`$0', `$1')$0(popdef(`$0')_$0')dnl
define(`_lambda', `$@)')dnl

Note that an anonymous macro is always called with at least one argument
(after all, a macro without arguments is invoked by listing the macro name
without a starting '(', but an anonymous macro has no name).  Put another
way, the expansion of lambda is an unnamed function that expects further
arguments.

lambda(`$2-$1')(`a', `b')
=> b-a

An anonymous macro can be given a name; although care must be taken to
defer the substitution of parameters like $1 to the expansion of the
anonymous function, rather than the expansion of the named wrapper.

define(`swap', `lambda(`$'`2-$'`1')($@)')dnl
swap(`a', `b')
=> b-a

However, the real power of anonymous functions is in higher-order
functional programming.  [here would be where I work on an exposition of
the Y combinator, and prove that recursion without the use of $0 is
possible thanks to lambda and Y]

[I should probably also discuss whether m4 uses applicative or normal
order - at face value, m4 is applicative (arguments are expanded prior to
the macro expansion), however, thanks to quoting, m4 is often used with
normal expansion (by quoting your arguments, they are not expanded until
the calling macro is complete, particularly important with macros that
expand to an ifelse test)]

- --
Don't work too hard, make some time for fun as well!

Eric Blake             address@hidden
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAklwlNwACgkQ84KuGfSFAYDy4gCgkcdzZZMBGRMEAzUqkAyqzfTT
oz0AniiOFC2Xtvx2Oq36ttFxz5J9X8Jr
=/4uB
-----END PGP SIGNATURE-----




reply via email to

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