How can I write code using FP

kernel1983 kernel1983 at gmail.com
Tue Jan 16 23:34:23 EST 2007


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... at physics.uq.edu.au> wrote:
> 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




More information about the Python-list mailing list