Skip to content

Instantly share code, notes, and snippets.

@44100hertz
Created June 2, 2022 14:12
Show Gist options
  • Save 44100hertz/8ecedf5c68c22d30fa23cc5bd623dbc9 to your computer and use it in GitHub Desktop.
Save 44100hertz/8ecedf5c68c22d30fa23cc5bd623dbc9 to your computer and use it in GitHub Desktop.
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