Skip to content

Instantly share code, notes, and snippets.

@jamiehannaford
Last active August 29, 2015 14:02
Show Gist options
  • Save jamiehannaford/6dd30679a30e0b0f32f7 to your computer and use it in GitHub Desktop.
Save jamiehannaford/6dd30679a30e0b0f32f7 to your computer and use it in GitHub Desktop.
This style of coding in which we define a function without reference to its arguments — in some sense saying what a function *is* rather than what it *does* — is known as “point-free” style.
-- starting point
fun1 :: [Integer] -> Integer
fun1 [] = 1
fun1 (x:xs)
| even x = (x - 2) * fun1 xs
| otherwise = fun1 xs
-- incorporating existing recursion patterns
fun1' :: [Integer] -> Integer
fun1' = foldr (\x acc -> (if even x then x - 2 else 1) * acc) 1
-- much better
fun1'' :: [Integer] -> Integer
fun1'' = product . map (subtract 2) . filter even
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment