从书中Learn You a Haskell For Great Good偏函数的相关章节包含以下代码:什么是这些Haskell高阶函数的Python等价物?
multThree :: (Num a) => a -> a -> a -> a
multThree x y z = x * y * z
ghci> let multTwoWithNine = multThree 9
ghci> multTwoWithNine 2 3
54
ghci> let multWithEighteen = multTwoWithNine 2
ghci> multWithEighteen 10
180
我目前在Python中functools库中播放,并设法复制使用它的职能的行为。
from functools import partial
def multThree(x,y,z):
return x * y * z
>>> multTwoWithNine = partial(multThree,9)
>>> multTwoWithNine(2,3)
>>> multWithEighteen = partial(multTwoWithNine,2)
>>> multWithEighteen(10)
180
有一件事我现在想要做的就是看能不能从同一本书章节复制一些比较有意思的高阶功能,如:
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
不过,我米不知道如何做到这一点,或者如果partial()
甚至在这里有用。
您应该编辑和修复你的职责,确保他们都是语法正确。例如,你的第一个问题就是缺少它的论点。 – 2013-02-10 04:34:51
从技术上讲,你必须在任何转换中使用'partial',因为所有的Haskell函数都会自动执行curried,而'partial'模拟curried函数必须部分应用的能力。或者您可以将python版本编写为curried函数,但是您必须将它们称为“foo(a)(b)(c)'。 – Wes 2013-02-10 04:57:33
小调:你想要“部分应用功能”而不是“部分功能”。 – 2013-02-10 09:35:21