f1 :: Int -> Int -> Int   
f1 a b = a + b  
  
f2 :: Int -> (Int -> Int)   
f2 a b = a + b  
  
f3 :: Int -> (Int -> Int)   
(f3 a) b = a + b  
  
f1 == f2 == f3  

Beispiel

twice :: (a -> a) -> a -> a  
twice f x = f (f x)  
  
> twice (*2) 10  
40  

Funktionen höherer Ordnung für Listen

map :: (a -> b) -> [a] -> [b]  
map f xs = [f x | x <- xs]  
  
> map even [1..5]  
[False, True, False, True, False]  
filter :: (a -> Bool) -> [a] -> [a]  
filter p xs = [x | x <- xs, p x]  
  
> filter even [1..10]  
[2, 4, 6, 8, 10]  
> all even [2, 4, 8]  
True   
> any odd [2, 4, 9]  
True   
> takeWhile even [2, 4, 7, 8]  
[2, 4]  
> dropWhile even [2, 4, 7, 8]  
[7, 8]  

Strukturelle Rekursion

Bekanntes Muster für Rekursion über Listen:

f []     = v  
f (x:xs) = x # f xs  
  
sum []     = 0  
sum (x:xs) = x + sum xs  
  
product []     = 1  
product (x:xs) = x + product xs  

Wird zu

sum :: Num a => [a] -> a  
sum = foldr (+) 0  
  
product :: Num a => [a] -> a  
product = foldr (*) 1  
  
foldr :: (a -> b -> b) -> b -> [a] -> b  
foldr f v []     = v  
foldr f v (x:xs) = f x (foldr f v xs)  

Haskell Foldr vs Foldl

foldr (#) v [x0, x1, .. , xn] =  
	x0 # (x1 # (.. # (xn # v)))  
	  
foldl (#) v [x0, x1, .. , xn] =  
	(((x0 # x1) # ..) # xn) # v  


Link to original