By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,814 Members | 1,136 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,814 IT Pros & Developers. It's quick & easy.

How can I write code using FP

P: n/a
In Function Program,Language can use like this:

define a function:
f = lambda x,y:x,y

then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y

we should be able call f2 with one parameter:f2(2)
just return 1,2

but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?

thanks~~~
I'm a English beginner!
I wish somebody understand my meaning

Jan 17 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jan 16, 9:18 pm, "kernel1983" <kernel1...@gmail.comwrote:
In Function Program,Language can use like this:

define a function:
f = lambda x,y:x,y

then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y

we should be able call f2 with one parameter:f2(2)
just return 1,2

but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?

thanks~~~
I'm a English beginner!
I wish somebody understand my meaning
If I understand correctly, you can do:

f = lambda x, y: (x, y)
f2 = lambda y: f(1, y)

Jan 17 '07 #2

P: n/a
kernel1983 wrote:
In Function Program?Language can use like this:

define a function:
f = lambda x,y:x,y

then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y

we should be able call f2 with one parameter:f2(2)
just return 1,2

but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?
If you are using python 2.5, then you can use functools.partial, otherwise
lookup one of the many curry implementations, like the ones found in
http://aspn.activestate.com/ASPN/Coo...n/Recipe/52549

One found in the comments of that page by Scott David Daniels:

def curry(*args, **kwargs):
function, args = args[0], args[1:]
def result(*rest, **kwrest):
combined = kwargs.copy()
combined.update(kwrest)
return function(*args + rest, **combined)
return result
so, in your case:

f = lambda x,y: (x,y)
f2 = functools.partial(f,1)
>>f2(3)
1,3

Jan 17 '07 #3

P: n/a
yeah! that's easy! althought no so clear

a = lambda x,y:(x,y)
b = lambda y:a(1,y)

a(1,2)
(1,2)

b(2)
(1,2)

On 1月17日, 下午12时10分, Alejandro Dubrovsky
<dubrov...@physics.uq.edu.auwrote:
kernel1983 wrote:
In Function Program?Language can use like this:
define a function:
f = lambda x,y:x,y
then we use f to define another function:
f2 = f(1)
the f2 should equal to:
f2=lambda y:1,y
we should be able call f2 with one parameter:f2(2)
just return 1,2
but how can I implement the characteristic in Python??I think it could
do like this but HOW-TO?If you are using python 2.5, then you can use functools.partial, otherwise
lookup one of the many curry implementations, like the ones found inhttp://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549

One found in the comments of that page by Scott David Daniels:

def curry(*args, **kwargs):
function, args = args[0], args[1:]
def result(*rest, **kwrest):
combined = kwargs.copy()
combined.update(kwrest)
return function(*args + rest, **combined)
return result

so, in your case:

f = lambda x,y: (x,y)
f2 = functools.partial(f,1)>>f2(3)1,3
Jan 17 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.