> The ultimate example would be writing the Y combinator (a lambda
> function that implements the lambda function), or the related Z
> combinator.  I have tried in the past to accomplish that in m4 syntax,
> and failed in my attempts so far (although I haven't tried recently).
> So I would welcome anyone that can demonstrate a working Y combinator
> in m4 as a way to implement anonymous recursion.

(more for myself than anything else):

https://stackoverflow.com/questions/481692/can-a-lambda-function-call-itself-recursively-in-python

demonstrates how to write an anonymously recursive implementation of
factorial or fibonacci in python:

>>> (lambda f: (lambda x: f(lambda v: x(x)(v)))(lambda x: f(lambda v: 
>>> x(x)(v))))(lambda f: (lambda i: 1 if (i == 0) else i * f(i - 1)))(5)
120
>>> (lambda f: (lambda x: f(lambda v: x(x)(v)))(lambda x: f(lambda v: 
>>> x(x)(v))))(lambda f: (lambda i: f(i - 1) + f(i - 2) if i > 1 else 1))(5)
8

I would love to see someone successfully writing that same sort of
functionality in m4, to prove it can be done (and knowing full well
that it will probably execute slower than just directly defining a
named macro that can then invoke itself by name).

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org


Reply via email to