Skip to content

Instantly share code, notes, and snippets.

@StevenXL
Created March 19, 2017 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StevenXL/1592c92f6a32b97ff9c0e381237ca005 to your computer and use it in GitHub Desktop.
Save StevenXL/1592c92f6a32b97ff9c0e381237ca005 to your computer and use it in GitHub Desktop.
foldr
-- 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