473,473 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Not entirely serious: recursive lambda?

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/Coo.../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
Jul 20 '08 #1
8 1842
On Sat, Jul 19, 2008 at 10:43 PM, Michael Tobis <mt****@gmail.comwrote:
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
Jul 20 '08 #2
On Jul 19, 11:49*pm, Miles <semantic...@gmail.comwrote:
On Sat, Jul 19, 2008 at 10:43 PM, Michael Tobis <mto...@gmail.comwrote:
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
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
Jul 20 '08 #3
On 20 Jul., 04:43, Michael Tobis <mto...@gmail.comwrote:
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/Coo.../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 ;)
Jul 20 '08 #4
Kay Schluehr:
Sure, use a fixed point combinator. I've just added this recipe:
http://aspn.activestate.com/ASPN/Coo.../Recipe/576366
Does it work?

Bye,
bearophile
Jul 20 '08 #5
On 20 Jul., 13:08, bearophileH...@lycos.com wrote:
Kay Schluehr:
Sure, use a fixed point combinator. I've just added this recipe:
http://aspn.activestate.com/ASPN/Coo.../Recipe/576366

Does it work?

Bye,
bearophile
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.
Jul 20 '08 #6
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/lambd...quicksort.html
http://www.p-nand-q.com/python/stupi...da_tricks.html

</F>

Jul 20 '08 #7
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
Jul 22 '08 #8
On Jul 22, 11:52*am, Michael Tobis <mto...@gmail.comwrote:
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
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

Jul 22 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

53
by: Oliver Fromme | last post by:
Hi, I'm trying to write a Python function that parses an expression and builds a function tree from it (recursively). During parsing, lambda functions for the the terms and sub-expressions...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
181
by: Tom Anderson | last post by:
Comrades, During our current discussion of the fate of functional constructs in python, someone brought up Guido's bull on the matter: http://www.artima.com/weblogs/viewpost.jsp?thread=98196 ...
64
by: dmattis | last post by:
I am trying to write a recursive version of Power(x,n) that works by breaking n down into halves(where half of n=n/2), squaring Power(x,n/2), and multiplying by x again if n was odd, and to find a...
4
by: Xah Lee | last post by:
A Lambda Logo Tour (and why LISP languages using λ as logo should not be looked upon kindly) Xah Lee, 2002-02 Dear lispers, The lambda character λ, always struck a awe in me, as with...
267
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at...
23
by: Kaz Kylheku | last post by:
I've been reading the recent cross-posted flamewar, and read Guido's article where he posits that embedding multi-line lambdas in expressions is an unsolvable puzzle. So for the last 15 minutes...
8
by: birchb | last post by:
While working on type expressions I am rather stuck for a way to express recursive types. A simple example of this is a singly-linked list of integers. In some languages you have compiler syntax...
0
by: Cameron Simpson | last post by:
On 20Jul2008 00:08, Kay Schluehr <kay.schluehr@gmx.netwrote: | 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.