Created
June 2, 2022 14:12
-
-
Save 44100hertz/8ecedf5c68c22d30fa23cc5bd623dbc9 to your computer and use it in GitHub Desktop.
https://en.wikibooks.org/wiki/Haskell/Lists_III "Scans" exercise
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
scanr_rec :: (a -> b -> b) -> b -> [a] -> [b] | |
scanr_rec _ acc [] = [acc] | |
scanr_rec f acc (x:xs) = let next = scanr_rec f acc xs in f x (head next) : next | |
scanr_fold :: (a -> b -> b) -> b -> [a] -> [b] | |
scanr_fold f acc = foldr scanFold [acc] | |
where | |
scanFold x acc = f x (head acc) : acc | |
scanl_rec :: (b -> a -> b) -> b -> [a] -> [b] | |
scanl_rec f acc [] = [acc] | |
scanl_rec f acc (x:xs) = acc : scanl_rec f (f acc x) xs | |
scanl_fold :: (b -> a -> b) -> b -> [a] -> [b] | |
scanl_fold f acc = reverse . foldl scanFold [acc] | |
where | |
scanFold acc x = f (head acc) x : acc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment