niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » python
question about what lamda does
Post new topic   Reply to topic Page 2 of 2 [17 Posts] View previous topic :: View next topic
Goto page:  Previous  1, 2
Author Message
danielx
*nix forums beginner


Joined: 18 Jul 2006
Posts: 8

PostPosted: Thu Jul 20, 2006 9:02 pm    Post subject: Re: question about what lamda does Reply with quote

Bruno Desthuilliers wrote:
Quote:
danielx wrote:
(snip)

Python's lambda really can't be as powerful as Lisp's because Python
does not have expressions that do case analysis (this is not lambda's
fault, of course Wink. The reason is that you really want to put each
case on its own set of lines. This enhances readability at the expense
of terseness. Since Python's statements are terminated by a newline, it
would be rather awkward to have a kind of expression where good style
calls for it to be spread out accross multiple lines.

You can try to simulate these kinds expressions using into a list or
dictionary, but this becomes rather messy. I think the only way to get
this done properly is to use eval. For example:

def recursiveFunction(args):
... # do stuff...
choices = { True:"0", False:"recurisveFunction(newArgs)" }
return eval( choices[predicate] )

Why do you want to use eval here ?

The reason that you need eval is that you want to prevent any cases
from being executed until you decide which one you want.

What about:

def recursiveFunction(args):
... # do stuff...
... # that defines 'newArgs' and 'predicate' of course ...
return (recursiveFunction, lambda x: 0)[predicate](newArgs)

Sure, that works, but don't take things so literally. For instance, if
you have a bunch of cases, you might not way to apply the same set of
arguments to all of them.

Also, let's not get distracted from the main point about how doing case
analysis in an expression is ugly, making lambda's weaker in Python
than in the language which inspired them.

Quote:

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xiludom.gro'.split('@')])"
Back to top
bruno modulix
*nix forums Guru


Joined: 21 Feb 2005
Posts: 819

PostPosted: Fri Jul 21, 2006 1:03 pm    Post subject: Re: question about what lamda does Reply with quote

danielx wrote:
Quote:
Bruno Desthuilliers wrote:

danielx wrote:
(snip)


Python's lambda really can't be as powerful as Lisp's because Python
does not have expressions that do case analysis (this is not lambda's
fault, of course Wink. The reason is that you really want to put each
case on its own set of lines.

An expression can span several lines.

Quote:
This enhances readability at the expense
of terseness. Since Python's statements are terminated by a newline,

or by a ';'

Quote:
it
would be rather awkward to have a kind of expression where good style
calls for it to be spread out accross multiple lines.

I must be pretty dumb, but I don't see how this relate to the problem of
case analysis in lambda expressions ?

Quote:
You can try to simulate these kinds expressions using into a list or
dictionary, but this becomes rather messy. I think the only way to get
this done properly is to use eval. For example:

def recursiveFunction(args):
... # do stuff...
choices = { True:"0", False:"recurisveFunction(newArgs)" }
return eval( choices[predicate] )

Why do you want to use eval here ?


The reason that you need eval is that you want to prevent any cases
from being executed until you decide which one you want.

What about:

def recursiveFunction(args):
... # do stuff...
... # that defines 'newArgs' and 'predicate' of course ...
return (recursiveFunction, lambda x: 0)[predicate](newArgs)


Sure, that works, but don't take things so literally.

Sorry for being pragmatic !-)

Quote:
For instance, if
you have a bunch of cases, you might not way to apply the same set of
arguments to all of them.

return {
'case1' : lambda: someFunc(args1),
'case2' : lambda: someFunc(args2),
'case3' : lambda: someOtherFunc(args1, arg42),
}.get(predicate, lambda: 0)()

Still no need for eval()...

Now of course there are limits to the exercice, and we're still far away
from ML-like pattern matching or Lisp 'case' forms. As you noted, Python
is a statement-based language, not an expression-based one like Lisp.
This makes a definitive difference.

Quote:
Also, let's not get distracted from the main point about how doing case
analysis in an expression is ugly,

Ugliness is in the eyes of the beholder <wink>

Quote:
making lambda's weaker in Python
than in the language which inspired them.

The fact is that Python "lambdas" are *not* Lisp lambdas. Python
"lambdas" are mostly a handy trick to turn a *simple* expression into a
closure - and definitively not a basic building block of the language.

Daniel, I of course do agree that Python lambdas are nothing near Lisp
lambdas - FWIW, Python is not Lisp neither -, but that looks like an
apple and banana comparison to me... IMHO, the most obvious problem with
Python lambdas is the word "lambda" !-)


--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xiludom.gro'.split('@')])"
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 2 of 2 [17 Posts] Goto page:  Previous  1, 2
View previous topic :: View next topic
The time now is Fri Nov 21, 2008 7:22 pm | All times are GMT
navigation Forum index » Programming » python
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts configuration question for httpd Karl Wang Apache 1 Fri Jul 21, 2006 2:10 pm
No new posts nim problem/question Ron AIX 0 Fri Jul 21, 2006 1:57 pm
No new posts question for JAVA developer who r using postgres sql as b... deepak pal PostgreSQL 1 Fri Jul 21, 2006 9:00 am
No new posts Encryption Question dtuttle1@gmail.com Berkeley DB 2 Thu Jul 20, 2006 10:09 pm

Remortgages | Free phpBB forum | Credit Cards | Loan | Property for sale in Spain
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.4698s ][ Queries: 16 (0.3499s) ][ GZIP on - Debug on ]