473,396 Members | 1,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Lambda in python

Xx r3negade
Hi, I am trying to convert the following procedural function into a lambda function:
Expand|Select|Wrap|Line Numbers
  1. def inc_e(e):
  2.     if e < 10:
  3.         e = e + 1
  4.         return inc_e(e)
  5.     else:
  6.         return e
  7.  
  8. f = inc_e(0)
  9. print f
  10.  
This is what I have as my functional equivalent:
Expand|Select|Wrap|Line Numbers
  1. inc_e = lambda e:e < 10 and (e + 1, inc_e(e)) or e
  2. print inc_e
  3.  
The output of inc_e is:
Expand|Select|Wrap|Line Numbers
  1. <function <lambda> at 0x83ca28>
  2.  
So what exactly is my lambda function returning? If it is returning inc_e(e), then why isn't the program just looping over and over?
Jun 2 '08 #1
3 1604
jlm699
314 100+
You need to call inc_e now with something like inc_e(5)
Jun 2 '08 #2
Okay, but now I get a "maximum recursion depth exceeded" error. Why is it not returning e when e is greater than 10?
Jun 2 '08 #3
Your problem is with the "e + 1" part.

Expand|Select|Wrap|Line Numbers
  1. >>> e = 1
  2. >>> e + 1
  3. 2
  4. >>> e
  5. 1
  6. >>>
A working version of your lambda is here:

Expand|Select|Wrap|Line Numbers
  1. inc_e = lambda e:e < 10 and (inc_e(e+1)) or e
Jun 5 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: mike420 | last post by:
Earlier Ed Schofield (thanks, man) warned us that flist = for i in range(3) f = lambda x: x + i flist.append(f)
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...
63
by: Stephen Thorne | last post by:
Hi guys, I'm a little worried about the expected disappearance of lambda in python3000. I've had my brain badly broken by functional programming in the past, and I would hate to see things...
26
by: Steven Bethard | last post by:
I thought it might be useful to put the recent lambda threads into perspective a bit. I was wondering what lambda gets used for in "real" code, so I grepped my Python Lib directory. Here are some...
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 ...
25
by: Russell | last post by:
I want my code to be Python 3000 compliant, and hear that lambda is being eliminated. The problem is that I want to partially bind an existing function with a value "foo" that isn't known until...
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...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
21
by: globalrev | last post by:
i have a rough understanding of lambda but so far only have found use for it once(in tkinter when passing lambda as an argument i could circumvent some tricky stuff). what is the point of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...

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.