|   Grundlagen der Funktionalen Programmierung: Funktionen höherer Ordnung |  | 
| 
 | 
| 
 | 
| 
 | 
|    1module HigherOrderedFct    2where    3    4import Prelude hiding ( map    5                      , filter    6                      , any    7                      , all    8                      , takeWhile    9                      , dropWhile   10                      , sum   11                      , product   12                      , or   13                      , and   14                      , concat   15                      , reverse   16                      , foldl   17                      , foldr   18                      , flip   19                      , id   20                      , (.)   21                      )   22   23twice           :: (a -> a) -> a -> a   24twice f x       = f (f x)   25   26map             :: (a -> b) -> [a] -> [b]   27map f xs        = [f x | x <- xs]   28   29   30map' :: (a -> b) -> [a] -> [b]   31map' f []       = []   32map' f (x : xs) = f x : map' f xs   33   34   35filter          :: (a -> Bool) -> [a] -> [a]   36filter p xs     = [x | x <- xs, p x]   37   38   39filter'         :: (a -> Bool) -> [a] -> [a]   40filter' p []    = []   41filter' p (x : xs)   42    | p x       = x : filter' p xs   43    | otherwise =     filter' p xs   44   45   46all            :: (a -> Bool) -> [a] -> Bool   47all p []       = True   48all p (x : xs) = p x && all p xs   49   50   51any            :: (a -> Bool) -> [a] -> Bool   52any p []       = False   53any p (x : xs) = p x || any p xs   54   55   56takeWhile       :: (a -> Bool) -> [a] -> [a]   57takeWhile p []  = []   58takeWhile p (x : xs)   59    | p x       = x : takeWhile p xs   60    | otherwise = []   61   62   63dropWhile :: (a -> Bool) -> [a] -> [a]   64dropWhile p []  = []   65dropWhile p (x : xs)   66    | p x       = dropWhile p xs   67    | otherwise = x : xs   68   69-- foldr   70   71sum             :: Num a => [a] -> a   72sum []          = 0   73sum (x : xs)    = x + sum xs   74   75product         :: Num a => [a] -> a   76product []      = 1   77product (x : xs)   78                = x * product xs   79   80or              :: [Bool] -> Bool   81or []           = False   82or (x : xs)     = x || or xs   83   84and             :: [Bool] -> Bool   85and []          = True   86and (x : xs)    = x && and xs   87   88concat          :: [[a]] -> [a]   89concat []       = []   90concat (xs : xss)   91                = xs ++ concat xss   92   93reverse         :: [a] -> [a]   94reverse []      = []   95reverse (x : xs)= reverse xs ++ [x]   96   97sumR            :: Num a => [a] -> a   98sumR            = foldr (+)  0   99  100productR        :: Num a => [a] -> a  101productR        = foldr (*)  1  102  103orR             :: [Bool] -> Bool  104orR             = foldr (||) False  105  106andR            :: [Bool] -> Bool  107andR            = foldr (&&) True  108  109concatR         :: [[a]] -> [a]  110concatR         = foldr (++) []  111  112  113foldr           :: (a -> b -> b) -> b -> [a] -> b  114foldr op c []   = c  115foldr op c (x : xs)  116                = x `op` foldr op c xs  117  118lengthR         :: [a] -> Integer  119lengthR         = foldr (\ x n -> 1 + n) 0  120  121reverseR        :: [a] -> [a]  122reverseR        = foldr (\ x xs -> xs ++ [x]) []  123  124mapR            :: (a -> b) -> [a] -> [b]  125mapR f          = foldr (\ x xs -> f x : xs) []  126  127filterR         :: (a -> Bool) -> [a] -> [a]  128filterR p       = foldr (\ x xs -> if p x  129                                   then x : xs  130                                   else xs  131                  ) []  132  133suml            :: Num a => [a] -> a  134suml            = sum' 0  135    where  136      sum' acc []       = acc  137      sum' acc (x : xs) = sum' (acc + x) xs  138  139reverse'        :: [a] -> [a]  140reverse' xs     = rev [] xs  141  where  142    rev acc []       = acc  143    rev acc (x : xs) = rev (x : acc) xs  144  145foldl               :: (b -> a -> b) -> b -> [a] -> b  146foldl op acc []     = acc  147foldl op acc (x:xs) = foldl op (acc `op` x) xs  148  149sumL            :: Num a => [a] -> a  150sumL            = foldl (+)  0  151  152productL        :: Num a => [a] -> a  153productL        = foldl (*)  1  154  155orL             :: [Bool] -> Bool  156orL             = foldl (||) False  157  158andL            :: [Bool] -> Bool  159andL            = foldl (&&) True  160  161concatL         :: [[a]] -> [a]  162concatL         = foldl (++) []  163  164lengthL         :: [b] -> Integer  165lengthL         = foldl (\ r x -> r + 1) 0  166  167reverseL        :: [a] -> [a]  168reverseL        = foldl (\ r x -> x : r) []  169  170-- ghci: :set +s  171  172t1 = length (reverseR [1..10000])  173t2 = length (reverseL [1..10000])  174t3 = length (concatR (replicate 10000 "a"))  175t4 = length (concatL (replicate 10000 "a"))  176  177infixr 9 .  178  179(.) :: (b -> c) -> (a -> b) -> (a -> c)  180f . g = \ x -> f (g x)  181  182id :: a -> a  183id = \ x -> x  184  185compose :: [a -> a] -> (a -> a)  186compose = foldr (.) id  187  188(>>>) :: (a -> b) -> (b -> c) -> (a -> c)  189(>>>) = flip (.)  190  191composeL :: [a -> a] -> (a -> a)  192composeL = foldl (>>>) id  193  194flip :: (a -> b -> c) -> (b -> a -> c)  195flip op = \ y x -> x `op` y  196  197fs :: [Integer -> Integer]  198fs = [(+1), (*2), (`div` 3), (^2)]  199  200r1 = compose  fs 5  201r2 = composeL fs 5 | 
| Letzte Änderung: 27.11.2019 | © Prof. Dr. Uwe Schmidt  |