- Open src/Main.hs and add the following definitions to it. For each function such as sum, we will create two versions--sumr, which uses foldr, and suml, which uses foldl.
- Use both, right and left folds to sum up the numerical contents of a list to write functions sumr and suml as follows:
sumr :: Num a => [a] -> a sumr xs = foldr (+) 0 xs suml :: Num a => [a] -> a suml xs = foldl (+) 0 xs
- Similarly, use right and left folds to calculate product of all elements in the list. This should results in functions productr and productl respectively:
productr :: Num a => [a] -> a productr xs = foldr (*) 1 xs productl :: Num a => [a] -> a productl xs = foldl (*) 1 xs
- Define map using the folds mapr and mapl:
mapr :: (a -> b) ...