Last active
February 3, 2016 22:51
-
-
Save tntmarket/2a39664a5aeb46c7e43f to your computer and use it in GitHub Desktop.
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
import Data.Char (toUpper) | |
-- space is high priority function application: | |
-- f x = f(x) in other languages | |
-- functions are defined like this: | |
-- f x = x * 2 | |
-- is equivalent to | |
-- def f(x): return x * 2 | |
-- to say f is SomeType: | |
-- f :: SomeType | |
-- $ is low priority function application: | |
-- f (g (h x))) = f $ g $ h x | |
-- IO () means "An IO action that doesn't return anything" | |
main :: IO () | |
main = putStrLn $ reverseCapitalize "asdf qwerty zxcv" -- prints Zxcv Qwerty Asdf | |
-- pointfree | |
reverseCapitalize :: String -> String | |
reverseCapitalize = unwords . reverse . map capitalize . words | |
-- pointful | |
reverseCapitalize' :: String -> String | |
reverseCapitalize' s = unwords $ reverse $ map capitalize $ words s | |
-- you can define piecewise functions | |
-- f 1 = "One" | |
-- f 2 = "Two" | |
-- f x = "Anything else" | |
-- and pattern match on arguments, like how python lets you do | |
-- x, y = [1, 2] | |
capitalize :: String -> String | |
capitalize "" = "" | |
capitalize (first:rest) = toUpper first : rest | |
-- : is the list prepend operator | |
-- 0:[1] = [0, 1] | |
-- in general | |
-- f x = blahBlah x | |
-- reduces to | |
-- f = blahBlah | |
-- see also https://wiki.haskell.org/Pointfree |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment