0
Functional Programming
Input/Output
H. Turgut Uyar
2013-2015
License
© 2013-2015 H. Turgut Uyar
You are free to:
Share – copy and redistribute the material in any medium or format
Ada...
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
I/O Model
how can I/O fit into the functional model?
how about a function that reads in a value of the desired type
from t...
I/O Model
how can I/O fit into the functional model?
how about a function that reads in a value of the desired type
from t...
I/O Type
new type: IO a
a program which will do some I/O and return a value of type a
instead of:
inputInt :: Integer
we h...
I/O Type
new type: IO a
a program which will do some I/O and return a value of type a
instead of:
inputInt :: Integer
we h...
I/O Type
if I/O doesn’t produce a result: IO ()
output:
putStr :: String -> IO ()
putStrLn :: String -> IO ()
input:
getLi...
Program Start
entry point of the program: main
example: Hello, world!
main :: IO ()
main = putStrLn "Hello, world!"
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
String Conversions
convert a type to string: show
convert a string to another type: read
examples
show 42 ~> "42"
show 3.1...
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
Action Sequences
I/O consists of actions happening in a sequence
create an action sequence: do
small imperative programmin...
Sequence Example
print a string 4 times
put4times :: String -> IO ()
put4times str = do
putStrLn str
putStrLn str
putStrLn...
Capturing Values
capture value produced by the program: <-
can only be used within the sequence
example: reverse and print...
Local Definitions
local definitions: let
can only be used within the sequence
example: reverse two lines
reverse2lines :: ...
Returning Values
returning result of sequence: return
example: read an integer from the input
getInteger :: IO Integer
get...
Recursion in Sequence
copy input to output indefinitely
copy :: IO ()
copy = do
line <- getLine
putStrLn line
copy
Conditional in Sequence
copy input to output a number of times
copyN :: Integer -> IO ()
copyN n =
if n <= 0
then return (...
Conditional in Sequence
copy until input line is empty
copyEmpty :: IO ()
copyEmpty = do
line <- getLine
if line == ""
the...
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
Rock - Paper - Scissors
two players repeatedly play Rock-Paper-Scissors
data types
data Move = Rock | Paper | Scissors
der...
Outcome
outcome of one round
A wins → 1, B wins → -1, tied → 0
outcome :: Move -> Move -> Integer
outcome moveA moveB = ca...
Outcome
outcome of one round
A wins → 1, B wins → -1, tied → 0
outcome :: Move -> Move -> Integer
outcome moveA moveB = ca...
String Conversions
convert a round in the game to string
showRound :: (Move, Move) -> String
showRound (moveA, moveB) = "A...
String Conversions
convert a round in the game to string
showRound :: (Move, Move) -> String
showRound (moveA, moveB) = "A...
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
Strategies
a strategy is a function that selects a move
based on the previous moves of the opponent:
[Move] -> Move
always...
Strategies
cycle through the possibilities
cycle :: [Move] -> Move
cycle moves = case (length moves) ‘mod‘ 3 of
0 -> Rock
...
Strategies
play whatever the opponent played last
-- opponent moves kept in reverse order
echo :: [Move] -> Move
echo [] =...
Topics
1 I/O Model
Introduction
String Conversions
Action Sequences
2 Example: Rock - Paper - Scissors
Data Types
Strategi...
Game Play
play the game interactively
convert a character into a move
convertMove :: Char -> Move
convertMove c
| c ‘elem‘...
Game Play
play interactively against echo
play :: Match -> IO ()
play (movesA, movesB) = do
ch <- getChar
putStrLn ""
if c...
Automatic Play
generate a match: cycle versus echo
generateMatch :: Integer -> Match
generateMatch 0 = ([], [])
generateMa...
References
Required Reading: Thompson
Chapter 8: Playing the game: I/O in Haskell
Upcoming SlideShare
Loading in...5
×

Functional Programming - Input/Output

242

Published on

I/O in Haskell.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
242
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Functional Programming - Input/Output"

  1. 1. Functional Programming Input/Output H. Turgut Uyar 2013-2015
  2. 2. License © 2013-2015 H. Turgut Uyar You are free to: Share – copy and redistribute the material in any medium or format Adapt – remix, transform, and build upon the material Under the following terms: Attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. NonCommercial – You may not use the material for commercial purposes. ShareAlike – If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. For more information: https://creativecommons.org/licenses/by-nc-sa/4.0/ Read the full license: https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
  3. 3. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  4. 4. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  5. 5. I/O Model how can I/O fit into the functional model? how about a function that reads in a value of the desired type from the input? inputInt :: Integer breaks reasoning: inputDiff = inputInt - inputInt any function might be affected: foo :: Integer -> Integer foo n = inputInt + n
  6. 6. I/O Model how can I/O fit into the functional model? how about a function that reads in a value of the desired type from the input? inputInt :: Integer breaks reasoning: inputDiff = inputInt - inputInt any function might be affected: foo :: Integer -> Integer foo n = inputInt + n
  7. 7. I/O Type new type: IO a a program which will do some I/O and return a value of type a instead of: inputInt :: Integer we have: inputInt :: IO Integer no longer valid: inputInt - inputInt inputInt + n
  8. 8. I/O Type new type: IO a a program which will do some I/O and return a value of type a instead of: inputInt :: Integer we have: inputInt :: IO Integer no longer valid: inputInt - inputInt inputInt + n
  9. 9. I/O Type if I/O doesn’t produce a result: IO () output: putStr :: String -> IO () putStrLn :: String -> IO () input: getLine :: IO String
  10. 10. Program Start entry point of the program: main example: Hello, world! main :: IO () main = putStrLn "Hello, world!"
  11. 11. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  12. 12. String Conversions convert a type to string: show convert a string to another type: read examples show 42 ~> "42" show 3.14 ~> "3.14" read "42" :: Integer ~> 42 read "42" :: Float ~> 42.0 read "3.14" :: Float ~> 3.14
  13. 13. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  14. 14. Action Sequences I/O consists of actions happening in a sequence create an action sequence: do small imperative programming language do action1 action2 ...
  15. 15. Sequence Example print a string 4 times put4times :: String -> IO () put4times str = do putStrLn str putStrLn str putStrLn str putStrLn str
  16. 16. Capturing Values capture value produced by the program: <- can only be used within the sequence example: reverse and print the line read from the input reverseLine :: IO () reverseLine = do line <- getLine putStrLn (reverse line)
  17. 17. Local Definitions local definitions: let can only be used within the sequence example: reverse two lines reverse2lines :: IO () reverse2lines = do line1 <- getLine line2 <- getLine let rev1 = reverse line1 let rev2 = reverse line2 putStrLn rev2 putStrLn rev1
  18. 18. Returning Values returning result of sequence: return example: read an integer from the input getInteger :: IO Integer getInteger = do line <- getLine return (read line :: Integer)
  19. 19. Recursion in Sequence copy input to output indefinitely copy :: IO () copy = do line <- getLine putStrLn line copy
  20. 20. Conditional in Sequence copy input to output a number of times copyN :: Integer -> IO () copyN n = if n <= 0 then return () else do line <- getLine putStrLn line copyN (n - 1)
  21. 21. Conditional in Sequence copy until input line is empty copyEmpty :: IO () copyEmpty = do line <- getLine if line == "" then return () else do putStrLn line copyEmpty
  22. 22. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  23. 23. Rock - Paper - Scissors two players repeatedly play Rock-Paper-Scissors data types data Move = Rock | Paper | Scissors deriving Show type Match = ([Move], [Move]) -- ex: ([Rock, Rock, Paper], [Scissors, Paper, Rock])
  24. 24. Outcome outcome of one round A wins → 1, B wins → -1, tied → 0 outcome :: Move -> Move -> Integer outcome moveA moveB = case (moveA, moveB) of (Rock, Scissors) -> 1 (Scissors, Rock) -> -1 (Paper, Rock) -> 1 (Rock, Paper) -> -1 (Scissors, Paper) -> 1 (Paper, Scissors) -> -1 _ -> 0 exercise: determine the outcome of a match matchOutcome [Rock, Paper] [Paper, Scissors] ~> -2
  25. 25. Outcome outcome of one round A wins → 1, B wins → -1, tied → 0 outcome :: Move -> Move -> Integer outcome moveA moveB = case (moveA, moveB) of (Rock, Scissors) -> 1 (Scissors, Rock) -> -1 (Paper, Rock) -> 1 (Rock, Paper) -> -1 (Scissors, Paper) -> 1 (Paper, Scissors) -> -1 _ -> 0 exercise: determine the outcome of a match matchOutcome [Rock, Paper] [Paper, Scissors] ~> -2
  26. 26. String Conversions convert a round in the game to string showRound :: (Move, Move) -> String showRound (moveA, moveB) = "A plays: " ++ show moveA ++ ", B plays: " ++ show moveB exercise: convert match result to string showResult [Rock, Paper] [Paper, Scissors] ~> "Player B wins by 2" showResult [Rock, Paper] [Paper, Rock] ~> "It’s a tie"
  27. 27. String Conversions convert a round in the game to string showRound :: (Move, Move) -> String showRound (moveA, moveB) = "A plays: " ++ show moveA ++ ", B plays: " ++ show moveB exercise: convert match result to string showResult [Rock, Paper] [Paper, Scissors] ~> "Player B wins by 2" showResult [Rock, Paper] [Paper, Rock] ~> "It’s a tie"
  28. 28. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  29. 29. Strategies a strategy is a function that selects a move based on the previous moves of the opponent: [Move] -> Move always play the same move rock, paper, scissors :: [Move] -> Move rock _ = Rock paper _ = Paper scissors _ = Scissors
  30. 30. Strategies cycle through the possibilities cycle :: [Move] -> Move cycle moves = case (length moves) ‘mod‘ 3 of 0 -> Rock 1 -> Paper 2 -> Scissors
  31. 31. Strategies play whatever the opponent played last -- opponent moves kept in reverse order echo :: [Move] -> Move echo [] = Rock echo (latest:rest) = latest
  32. 32. Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play
  33. 33. Game Play play the game interactively convert a character into a move convertMove :: Char -> Move convertMove c | c ‘elem‘ "rR" = Rock | c ‘elem‘ "pP" = Paper | c ‘elem‘ "sS" = Scissors | otherwise = error "unknown move"
  34. 34. Game Play play interactively against echo play :: Match -> IO () play (movesA, movesB) = do ch <- getChar putStrLn "" if ch == ’.’ then putStrLn (showResult (movesA, movesB)) else do let moveA = echo movesB let moveB = convertMove ch putStrLn (showRound (moveA, moveB)) play (moveA : movesA, moveB : movesB) playInteractive :: IO () playInteractive = play ([], [])
  35. 35. Automatic Play generate a match: cycle versus echo generateMatch :: Integer -> Match generateMatch 0 = ([], []) generateMatch n = step (generateMatch (n - 1)) where step :: Match -> Match step (movesA, movesB) = (cycle movesB : movesA, echo movesA : movesB)
  36. 36. References Required Reading: Thompson Chapter 8: Playing the game: I/O in Haskell
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×