haskell-高阶函数1

haskell-高阶函数1

简单定义:就是接收函数的参数也可以返回函数作为结果

当我门以不全参数调用函数时,就可以得到一个不全的函数

实例:


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


完整例子


 applyTwice :: (a -> a) -> a -> a
 applyTwice f x = f (f x)
 
 zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
 zipWith' _ [] _ = []
 zipWith' _ _ [] = []
 zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys
 
 
 flip' :: (a -> b -> c) -> (b -> a -> c)
 flip' f = g
    where g x y = f y x
 
 map' ::(a->b) -> [a] -> [b]
 map' _ [] = []
 map' f(x:xs) = f x : map f xs
 
 filter' :: (a -> Bool) -> [a] -> [a]
 filter' _ [] = []
 filter' p (x:xs)
    | p x = x : filter p xs
    | otherwise = filter p xs

Loading Disqus comments...
Table of Contents