The chapter on Partial Functions from the book Learn You a Haskell For Great Good contains the following code:
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
I am currently playing with the functools library in Python, and managed to replicate the behavior of those functions using it.
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
One thing I would now like to do is see if I can replicate some of the more interesting higher-order functions from the same book chapter, such as:
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ [] _ = []
zipWith' _ _ [] = []
zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
However, I'm not sure how to do this, or if partial()
is even useful here.
partial
in any conversion, since all Haskell functions are automatically curried, andpartial
emulates the ability curried functions have to be partially applied. Or you can write the python versions as curried functions, but then you have to call them likefoo(a)(b)(c)
. – Wes