3. List
Type-parameter
Constructor
• data [a] = [] | a : [a]
Case distinction
Type
Constructor
4. Functions
Give me a list of a And I’ll return a
list of a
• reverse :: [a] -> [a]
• reverse [] = []
• reverse (x:xs) = reverse xs ++ [x]
Pattern Matching
6. Purity
• Same Input
• ntial
Refere cy
Transparen
=
• Same output
7. Purity
• No side effects:
• * Variables / Mutable state
• * I/O
• * launchMissiles()
8. Example
• sort :: [Int] -> [Int]
How do we know sort doesn’t launch missiles?
9. Doing I/O
• putStrLn :: String -> IO ()
The IO type shows us it’s not pure
10. Doing I/O
(>>=) :: IO a -> (a -> IO b) -> IO b
main = putStrLn "hello! what's your name?"
>>= () -> getLine
>>= name -> putStrLn ("hello, " ++ name)
11. Doing I/O
main = do
putStrLn "hello! what's your name?"
name <- getLine
putStrLn ("hello, " ++ name)