Anonymous user
Talk:Partial function application: Difference between revisions
→Proposal for new task description: new section
(→Proposal for new task description: new section) |
|||
Line 173:
And in the D version fs() is a templated function. It's the way you write generic code in such languages.
== Proposal for new task description ==
I would propose to extend the task description here include both currying and partial application. Here are samples in Haskell and Python:
<source lang=haskell>
module Main where
curry3 :: ((a, b, c) -> r) -> a -> b -> c -> r
curry3 f x y z = f (x, y, z)
papply2 :: ((a, b) -> r) -> a -> b -> r
papply2 f x = \y -> f (x, y)
papply3 :: ((a, b, c) -> r) -> a -> (b, c) -> r
papply3 f x = \(y, z) -> f (x, y, z)
f :: (Integer, Integer, Integer) -> Integer
f (a, b, x) = a * x + b
g :: Integer -> Integer -> Integer -> Integer
g a b x = a * x + b
g' :: Integer -> (Integer -> (Integer -> Integer))
g' = \a -> \b -> \x -> a * x + b
main = let u = curry3 f
v = papply2 (papply3 f 7) 9
w = g 7 9
in print [map (u 7 9) [1..5], map v [1..5], map w [1..5]]
</source>
<source lang=python>
from functools import partial
def curry3(f):
return lambda a: lambda b: lambda x: f(a, b, x)
def f(a, b, x):
return a * x + b
def g(a):
return lambda b: lambda x: a * x + b
def main():
u = curry3(f)
v = partial(f, 7, 9)
w = g(7)(9)
print [map(u(7)(9), [1,2,3,4,5]), map(v, [1,2,3,4,5]), map(w, [1,2,3,4,5])]
</source>
|