这本书《Haskell趣学指南》中的“偏函数”章节包含以下代码:
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()
是否在这里有用。
foo(a)(b)(c)
。 - Wes