Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
how to write function that returns function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Expand all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Bjorn Pettersen  
View profile  
 More options May 14 2002, 8:16 pm
Newsgroups: comp.lang.python
From: "Bjorn Pettersen" <BPetter...@NAREX.com>
Date: Tue, 14 May 2002 17:18:17 -0600
Local: Tues, May 14 2002 7:18 pm
Subject: RE: how to write function that returns function

> From: Paul Graham [mailto:s...@bugbear.com]

> I am not a Python expert, and I'm hoping someone
> can tell me how in Python to write a function
> of one argument x that returns a function of one
> argument y that returns x+y.

It's bad form to ask for help on your homework problems, after all *YOU*
are the one who is supposed to learn something...

> Here, in Scheme, is what I want to write:

> (define foo (x) (lambda (y) (+ x y)))

Well (+ x y) is spelled 'x + y' in Python.
(lambda (y) ...) is spelled 'lambda y: ...'
and (define foo (x) ...) is spelled

  def foo(x):
      ...

putting it all together you get:

  def foo(x):
      lambda y: x + y

very straight-forward. Now for some arcane matters.

If you're running Python 2.2+ you're done.
If you're running Python 2.1, you need to put 'from __future__ import
nested_scopes' at the top of your module.
If you're running Python 1.5.x, you need to change the lambda to:

  lambda y, x=x: x + y

the x=x creates a binding local to the lambda which was required for the
1.5.x versions.

[snip]

> [To reply to me directly please use pg@bug<remove>bear.com,
> removing the <remove>, because I don't check s...@bugbear.com.]

If Guido can deal with the spam he gets from posting here, so can you.

-- bjorn


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Fernando Rodríguez  
View profile  
(1 user)  More options May 15 2002, 2:56 pm
Newsgroups: comp.lang.python
From: Fernando Rodríguez <f...@easyjob.net>
Date: Wed, 15 May 2002 20:57:51 +0200
Local: Wed, May 15 2002 2:57 pm
Subject: Re: how to write function that returns function
On Tue, 14 May 2002 17:18:17 -0600, "Bjorn Pettersen" <BPetter...@NAREX.com>
wrote:

>> From: Paul Graham [mailto:s...@bugbear.com]

>> I am not a Python expert, and I'm hoping someone
>> can tell me how in Python to write a function
>> of one argument x that returns a function of one
>> argument y that returns x+y.

>It's bad form to ask for help on your homework problems, after all *YOU*
>are the one who is supposed to learn something...

True, but personally I consider far more irritating this pedantic attitude
that assumes that _all_ questions must be homework assignments. This is
unfortunately common in comp.lang.scheme and comp.lang.lisp. Let's keep it
away from comp.lang.python.

FYI: http://www.bugbear.com/bio.html

-----------------------
Fernando Rodriguez


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google