Created
March 19, 2017 14:04
-
-
Save StevenXL/1592c92f6a32b97ff9c0e381237ca005 to your computer and use it in GitHub Desktop.
foldr
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
| -- foldr is meant to encapsulate the following pattern: | |
| -- f [] = v | |
| -- f (x:xs) = x # f xs | |
| -- where # is an operator applied to the head of the list and recursively processing the tail | |
| -- Using sum: | |
| -- sum [] = 0 | |
| -- sum (x:xs) = x + sum xs | |
| -- Therefore, sum = foldr (+) 0 | |
| -- map f in terms of foldr: | |
| -- map f [] = [] | |
| -- map f (x:xs) = f x : map f xs | |
| -- Therefore, map f = foldr (\h rst -> f h : rst) [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment