22.03.16 - Interactive Programming
So far seen how Haskell can be used to write batch programs. Today, will see how to do interactive programs
Problem: Haskell programs have no side effects However interactive programs have side effects
Solution:
Can be written in Haskell by using types to distinguish pure expressions from impure actions that may involve side effects
IO a
- returns the value of type a
Basic Actions
getChar
Adjugate - reads a character from the keyboard, echos it to the screen and returns the character as a result
putChar c
- writes the character c to the screen and returns no result value
return v
- returns the value v without performing any interaction
Sequencing
Sequence of actions can be combined as a single composite action using the keyword do
Derived Primitives
getLine
- reads an entire string. Haskell reads each character one by one, then returns it as a list. Need to have an action in the do block
putStr
- Writes a string to the screen
putStrLn
- Writes a string and moving to a new line
Evaluating an action executes its side effects, with the final result value being discarded