Last active
August 29, 2015 14:02
-
-
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.
This file contains 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
-- 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