Created
October 17, 2018 08:53
-
-
Save ordoghl/08a30d3483ac72f3a7ca24869d80b1a0 to your computer and use it in GitHub Desktop.
Haskell foldl and foldr implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| foldl' :: (a -> b -> a) -> a -> [b] -> a | |
| foldl' fn acc [] = acc | |
| foldl' fn acc (x:xs) = foldl' fn (fn acc x) xs | |
| foldr' :: (a -> b -> b) -> b -> [a] -> b | |
| foldr' fn acc [] = acc | |
| foldr' fn acc (x:xs) = fn x (foldr' fn acc xs) | |
| f :: Double -> Double -> Double | |
| f = \acc x -> acc / x + 1 | |
| main :: IO () | |
| main = do | |
| print (foldl' f 0 [1,2,3]) | |
| print (foldr' f 0 [1,2,3]) | |
| print (foldr' (/) 1 [2,4,8]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@asarkar the
foldl'fromData.Listyou are referring to is indeed the strict version offoldlbut the original creator of this post was clearly referring to the standard versions offoldlandfoldras he also said in the title of the post.