Functional Programming - Input/Output
-
Upload
turgut-uyar -
Category
Education
-
view
239 -
download
1
description
Transcript of Functional Programming - Input/Output
![Page 1: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/1.jpg)
Functional ProgrammingInput/Output
H. Turgut Uyar
2013-2015
![Page 2: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/2.jpg)
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, andindicate 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 mustdistribute 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
![Page 3: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/3.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 4: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/4.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 5: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/5.jpg)
I/O Model
how can I/O fit into the functional model?
how about a function that reads in a value of the desired typefrom the input?inputInt :: Integer
breaks reasoning:inputDiff = inputInt - inputInt
any function might be affected:foo :: Integer -> Integerfoo n = inputInt + n
![Page 6: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/6.jpg)
I/O Model
how can I/O fit into the functional model?
how about a function that reads in a value of the desired typefrom the input?inputInt :: Integer
breaks reasoning:inputDiff = inputInt - inputInt
any function might be affected:foo :: Integer -> Integerfoo n = inputInt + n
![Page 7: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/7.jpg)
I/O Type
new type: IO aa 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 - inputIntinputInt + n
![Page 8: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/8.jpg)
I/O Type
new type: IO aa 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 - inputIntinputInt + n
![Page 9: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/9.jpg)
I/O Type
if I/O doesn’t produce a result: IO ()
output:putStr :: String -> IO ()putStrLn :: String -> IO ()
input:getLine :: IO String
![Page 10: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/10.jpg)
Program Start
entry point of the program: main
example: Hello, world!
main :: IO ()main = putStrLn "Hello, world!"
![Page 11: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/11.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 12: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/12.jpg)
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 ~> 42read "42" :: Float ~> 42.0read "3.14" :: Float ~> 3.14
![Page 13: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/13.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 14: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/14.jpg)
Action Sequences
I/O consists of actions happening in a sequence
create an action sequence: do
small imperative programming language
doaction1action2...
![Page 15: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/15.jpg)
Sequence Example
print a string 4 times
put4times :: String -> IO ()put4times str = do
putStrLn strputStrLn strputStrLn strputStrLn str
![Page 16: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/16.jpg)
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 <- getLineputStrLn (reverse line)
![Page 17: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/17.jpg)
Local Definitions
local definitions: let
can only be used within the sequence
example: reverse two lines
reverse2lines :: IO ()reverse2lines = do
line1 <- getLineline2 <- getLinelet rev1 = reverse line1let rev2 = reverse line2putStrLn rev2putStrLn rev1
![Page 18: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/18.jpg)
Returning Values
returning result of sequence: return
example: read an integer from the input
getInteger :: IO IntegergetInteger = do
line <- getLinereturn (read line :: Integer)
![Page 19: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/19.jpg)
Recursion in Sequence
copy input to output indefinitely
copy :: IO ()copy = do
line <- getLineputStrLn linecopy
![Page 20: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/20.jpg)
Conditional in Sequence
copy input to output a number of times
copyN :: Integer -> IO ()copyN n =
if n <= 0then return ()else do
line <- getLineputStrLn linecopyN (n - 1)
![Page 21: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/21.jpg)
Conditional in Sequence
copy until input line is empty
copyEmpty :: IO ()copyEmpty = do
line <- getLineif line == ""
then return ()else do
putStrLn linecopyEmpty
![Page 22: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/22.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 23: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/23.jpg)
Rock - Paper - Scissors
two players repeatedly play Rock-Paper-Scissors
data types
data Move = Rock | Paper | Scissorsderiving Show
type Match = ([Move], [Move])-- ex: ([Rock, Rock, Paper], [Scissors, Paper, Rock])
![Page 24: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/24.jpg)
Outcome
outcome of one roundA wins 7→ 1, B wins 7→ -1, tied 7→ 0
outcome :: Move -> Move -> Integeroutcome 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 matchmatchOutcome [Rock, Paper] [Paper, Scissors] ~> -2
![Page 25: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/25.jpg)
Outcome
outcome of one roundA wins 7→ 1, B wins 7→ -1, tied 7→ 0
outcome :: Move -> Move -> Integeroutcome 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 matchmatchOutcome [Rock, Paper] [Paper, Scissors] ~> -2
![Page 26: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/26.jpg)
String Conversions
convert a round in the game to string
showRound :: (Move, Move) -> StringshowRound (moveA, moveB) = "A plays: " ++ show moveA
++ ", B plays: " ++ show moveB
exercise: convert match result to stringshowResult [Rock, Paper] [Paper, Scissors]
~> "Player B wins by 2"
showResult [Rock, Paper] [Paper, Rock]~> "It’s a tie"
![Page 27: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/27.jpg)
String Conversions
convert a round in the game to string
showRound :: (Move, Move) -> StringshowRound (moveA, moveB) = "A plays: " ++ show moveA
++ ", B plays: " ++ show moveB
exercise: convert match result to stringshowResult [Rock, Paper] [Paper, Scissors]
~> "Player B wins by 2"
showResult [Rock, Paper] [Paper, Rock]~> "It’s a tie"
![Page 28: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/28.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 29: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/29.jpg)
Strategies
a strategy is a function that selects a movebased on the previous moves of the opponent:[Move] -> Move
always play the same move
rock, paper, scissors :: [Move] -> Moverock _ = Rockpaper _ = Paperscissors _ = Scissors
![Page 30: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/30.jpg)
Strategies
cycle through the possibilities
cycle :: [Move] -> Movecycle moves = case (length moves) ‘mod‘ 3 of
0 -> Rock1 -> Paper2 -> Scissors
![Page 31: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/31.jpg)
Strategies
play whatever the opponent played last
-- opponent moves kept in reverse orderecho :: [Move] -> Moveecho [] = Rockecho (latest:rest) = latest
![Page 32: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/32.jpg)
Topics
1 I/O ModelIntroductionString ConversionsAction Sequences
2 Example: Rock - Paper - ScissorsData TypesStrategiesGame Play
![Page 33: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/33.jpg)
Game Play
play the game interactively
convert a character into a move
convertMove :: Char -> MoveconvertMove c| c ‘elem‘ "rR" = Rock| c ‘elem‘ "pP" = Paper| c ‘elem‘ "sS" = Scissors| otherwise = error "unknown move"
![Page 34: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/34.jpg)
Game Play
play interactively against echo
play :: Match -> IO ()play (movesA, movesB) = do
ch <- getCharputStrLn ""if ch == ’.’
then putStrLn (showResult (movesA, movesB))else do
let moveA = echo movesBlet moveB = convertMove chputStrLn (showRound (moveA, moveB))play (moveA : movesA, moveB : movesB)
playInteractive :: IO ()playInteractive = play ([], [])
![Page 35: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/35.jpg)
Automatic Play
generate a match: cycle versus echo
generateMatch :: Integer -> MatchgenerateMatch 0 = ([], [])generateMatch n = step (generateMatch (n - 1))wherestep :: Match -> Matchstep (movesA, movesB) = (cycle movesB : movesA,
echo movesA : movesB)
![Page 36: Functional Programming - Input/Output](https://reader033.fdocuments.net/reader033/viewer/2022052900/5562ec6fd8b42a213b8b4c56/html5/thumbnails/36.jpg)
References
Required Reading: ThompsonChapter 8: Playing the game: I/O in Haskell