Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Not entirely serious: recursive lambda?

2 views
Skip to first unread message

Michael Tobis

unread,
Jul 19, 2008, 10:43:35 PM7/19/08
to
I came across the "japh" concept today and decided to do one of my
own, obviously, interpreting the 'p' somewhat loosely,

http://en.wikipedia.org/wiki/JAPH

but I'm not entirely satisfied with it:

####
# japh, for certain values of 'p'

f=lambda(r,N):N and f((" acdefijlmnopqrstuv"[N%19]+r,N/19))or(r,N)
print f( ("",reduce(lambda
c,m:c*95+''.join(map(chr,range(32,127))).index(m),
"!b)'1Mgh0z+fYQ]g::i^<&y~g)cnE-d=K&{GMNQ1gx+ooY<~L##N'X]P2<@XYXwX3z",
0)))[0]

####

it bothers me that there are two statements. (In case you are
wondering what they do, it's all essentially about changing from base
95 to base 19. It's based loosely on this fine, simple recipe by Drew
Perttula which I have found to be useful on several occasions:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/111286
)

Anyway, I'd much prefer an even uglier japh like this:

# does not work
print (lambda(r,N):N and $$$$((" acdefijlmnopqrstuv"[N%19]+r,N/
19))or(r,N))(
("",reduce(lambda c,m:c*95+''.join(map(chr,range(32,127))).index(m),
"!b)'1Mgh0z+fYQ]g::i^<&y~g)cnE-d=K&{GMNQ1gx+ooY<~L##N'X]P2<@XYXwX3z",
0)))[0]

but what would $$$$ be for an unnamed function to call itself?

I realize that lambda is something of an orphan and was arguably a bad
idea for anything besides obfuscation, but obfuscation is exactly my
purpose here. Can a lambda call itself without giving itself a name?
Google was not my friend on this one, and I suspect there is no
answer. Relax, I am not going to submit a PEP about it.

mt

Miles

unread,
Jul 20, 2008, 12:49:55 AM7/20/08
to pytho...@python.org
On Sat, Jul 19, 2008 at 10:43 PM, Michael Tobis <mto...@gmail.com> wrote:
> Can a lambda call itself without giving itself a name?

Kind of. There's a couple ways I know of.

The functional way, which involves the lambda receiving itself as an argument:

(lambda f: f(10, f))(lambda n, f: n and (sys.stdout.write("%d\n" % n)
or f(n-1,f)))

The stack frame examination way:

import sys, inspect, new
(lambda:sys.stdout.write('recurse\n') or
new.function(inspect.currentframe().f_code, globals())())()

The functional way is probably harder to grok unless you've studied
lambda calculus or had experience with "real" functional languages (I
haven't). For fun, try throwing a Y combinator in there.

-Miles

Paul McGuire

unread,
Jul 20, 2008, 1:11:55 AM7/20/08
to

Here is Michael Tobis's original program, using the functional
approach:

print (lambda f:f(("",reduce(lambda


c,m:c*95+''.join(map(chr,range(32,127))).index(m),
"!b)'1Mgh0z+fYQ]g::i^<&y~g)cnE-d=K&{GMNQ1gx
+ooY<~L##N'X]P2<@XYXwX3z",

0),f)))(lambda (r,N,f):N and f((" acdefijlmnopqrstuv"[N%19]+r,N/
19,f))or(r,N,f))[0]

Très assombri! (according to Babelfish...).

-- Paul

Kay Schluehr

unread,
Jul 20, 2008, 3:08:02 AM7/20/08
to
On 20 Jul., 04:43, Michael Tobis <mto...@gmail.com> wrote:

> Can a lambda call itself without giving itself a name?

Sure, use a fixed point combinator. I've just added this recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/576366

> Google was not my friend on this one, and I suspect there is no
> answer.

Even the Great Google can't help if you don't use the right
keywords ;)

bearoph...@lycos.com

unread,
Jul 20, 2008, 7:08:01 AM7/20/08
to
Kay Schluehr:

> Sure, use a fixed point combinator. I've just added this recipe:
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/576366

Does it work?

Bye,
bearophile

Kay Schluehr

unread,
Jul 20, 2008, 7:44:48 AM7/20/08
to

There are lots of informal derivations of the Y combinator on the web.
I used one a while ago and translated the result into Python. So if
there isn't an implementation bug it shall work.

I added two examples for illustration purposes.

Fredrik Lundh

unread,
Jul 20, 2008, 9:43:53 AM7/20/08
to pytho...@python.org
Michael Tobis wrote:

> I realize that lambda is something of an orphan and was arguably a bad
> idea for anything besides obfuscation, but obfuscation is exactly my
> purpose here. Can a lambda call itself without giving itself a name?
> Google was not my friend on this one, and I suspect there is no
> answer. Relax, I am not going to submit a PEP about it.

gerson kurz' writings on lambdaization might be helpful (or not):

http://www.p-nand-q.com/python/lambdaizing_quicksort.html
http://www.p-nand-q.com/python/stupid_lambda_tricks.html

</F>

Michael Tobis

unread,
Jul 22, 2008, 12:52:56 PM7/22/08
to
Thanks all! What a remarkable set of answers, intelligent, thought
provoking and informative without exception.

Of course, now I can't use Paul's version; it hardly counts as a japh
if someone else wrote it! It is probably the closest to my original
vision, alas. Miles' second suggestion was the one I was fumbling
toward; I will study it. No spoilers please.

best
mt

Paul McGuire

unread,
Jul 22, 2008, 1:36:14 PM7/22/08
to

Michael -

Sorry to spoil your fun - the concept of a recursive lambda was such
an interesting diversion, I couldn't resist! I'll try to restrain
myself next time.

-- Paul

Cameron Simpson

unread,
Jul 27, 2008, 7:10:58 PM7/27/08
to Kay Schluehr, pytho...@python.org
On 20Jul2008 00:08, Kay Schluehr <kay.sc...@gmx.net> wrote:
| > Google was not my friend on this one, and I suspect there is no
| > answer.
|
| Even the Great Google can't help if you don't use the right
| keywords ;)

Actually, I was shown an useful Google search syntax the other day:

Searching for:

foo

looks just for "foo", but searching for:

~foo

matches similar words.

This came up in a discussion of web sites that use badly chosen keywords (the
NSW Police gun licensing information talks only about "firearms", making it
surprisingly hard to find; not that I have or want a gun license, but it was
the example discussed).

Cheers,
--
Cameron Simpson <c...@zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Humans are incapable of securely storing high quality cryptographic
keys and they have unacceptable speed and accuracy when performing
cryptographic operations. (They are also large, expensive to maintain
diffcult to manage and they pollute the environment.) It is astonishing
that these devices continue to be manufactured and deployed. But they
are suffciently pervasive that we must design our protocols around
their limitations. - C Kaufman, R Perlman, M Speciner
_Network Security: PRIVATE Communication in a
PUBLIC World_, Prentice Hall, 1995, pp. 205.

0 new messages