Functional Programming - Input/Output

50 %
50 %
Information about Functional Programming - Input/Output
Education

Published on April 14, 2014

Author: uyar

Source: slideshare.net

Description

I/O in Haskell.

Functional Programming Input/Output H. Turgut Uyar 2013-2014

License © 2013-2014 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. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. Noncommercial – You may not use the material for commercial purposes. Share Alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. Legal code (the full license): https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

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

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

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

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

I/O Type if the result of the I/O is not significant: IO () output: putStr :: String -> IO () putStrLn :: String -> IO () input: getLine :: IO String

Program Start entry point of the program: main Hello, world! main :: IO () main = putStrLn "Hello, world!"

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

String Conversions to convert a type to string: show to convert a string to another type: read show 42 -- "42" show 3.14 -- "3.14" read "42" :: Integer -- 42 read "42" :: Float -- 42.0 read "3.14" :: Float -- 3.14

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

Action Sequences I/O consists of actions happening in a sequence to create an action sequence: do small imperative programming language do action1 action2 ...

Sequence Example print a string 4 times put4times :: String -> IO () put4times str = do putStrLn str putStrLn str putStrLn str putStrLn str

Capturing Values capturing value read from input: <- can only be used within do reverse and print the line read from the input reverseLine :: IO () reverseLine = do line <- getLine putStrLn (reverse line)

Local Definitions local definitions: let can only be used within do reverse2lines :: IO () reverse2lines = do line1 <- getLine line2 <- getLine let rev1 = reverse line1 let rev2 = reverse line2 putStrLn rev2 putStrLn rev1

Returning Values returning result of sequence: return getInteger :: IO Integer getInteger = do line <- getLine return (read line :: Integer)

Recursion in Sequence copy input to output indefinitely copy :: IO () copy = do line <- getLine putStrLn line copy

Conditional in Sequence copy n times copyN :: Integer -> IO () copyN n = if n <= 0 then return () else do line <- getLine putStrLn line copyN (n - 1)

Conditional in Sequence copy until input line is empty copyEmpty :: IO () copyEmpty = do line <- getLine if line == "" then return () else do putStrLn line copyEmpty

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

Data Types two players repeatedly play Rock - Paper - Scissors data Move = Rock | Paper | Scissors deriving Show type Match = ([Move], [Move]) -- ex: ([Rock, Rock, Paper], [Scissors, Paper, Rock])

Rock - Paper - Scissors a function that will determine the outcome of one round: 1 if player A wins, −1 if player B wins, 0 if tied outcome :: Move -> Move -> Integer outcome moveA moveB = case (moveA, moveB) of (Rock, Scissors) -> 1 (Scissors, Paper) -> 1 (Paper, Rock) -> 1 (Rock, Paper) -> -1 (Paper, Scissors) -> -1 (Scissors, Rock) -> -1 _ -> 0 exercise: write a function to determine the outcome of a match

Rock - Paper - Scissors a function that will determine the outcome of one round: 1 if player A wins, −1 if player B wins, 0 if tied outcome :: Move -> Move -> Integer outcome moveA moveB = case (moveA, moveB) of (Rock, Scissors) -> 1 (Scissors, Paper) -> 1 (Paper, Rock) -> 1 (Rock, Paper) -> -1 (Paper, Scissors) -> -1 (Scissors, Rock) -> -1 _ -> 0 exercise: write a function to determine the outcome of a match

Rock - Paper - Scissors outcome of a match matchOutcome :: Match -> Integer matchOutcome ([], []) = 0 matchOutcome (moveA:movesA, moveB:movesB) = outcome moveA moveB + matchOutcome (movesA, movesB)

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

Rock - Paper - Scissors a strategy is a function that selects a move based on the previous moves of the opponent: [Move] -> Move cycle through the possibilities cycle’ :: [Move] -> Move cycle’ moves = case (length moves) `mod` 3 of 0 -> Rock 1 -> Paper 2 -> Scissors

Rock - Paper - Scissors a strategy is a function that selects a move based on the previous moves of the opponent: [Move] -> Move cycle through the possibilities cycle’ :: [Move] -> Move cycle’ moves = case (length moves) `mod` 3 of 0 -> Rock 1 -> Paper 2 -> Scissors

Rock - Paper - Scissors strategy: play whatever the opponent played last keep the previous moves in reverse order (latest first) echo :: [Move] -> Move echo [] = Rock echo (latest:rest) = latest exercise: write the strategies where the player always plays the same move

Rock - Paper - Scissors strategy: play whatever the opponent played last keep the previous moves in reverse order (latest first) echo :: [Move] -> Move echo [] = Rock echo (latest:rest) = latest exercise: write the strategies where the player always plays the same move

Topics 1 I/O Model The Problem String Conversions Action Sequences 2 Example: Rock - Paper - Scissors Data Types Strategies Game Play

Rock - Paper - Scissors write a function that will let the user play the game interactively let the computer play the“echo”strategy playInteractive :: Match -> IO () playInteractive (movesA, movesB) = do ch <- getChar if not (ch `elem` "rpsRPS") then putStrLn (showResult (movesA, movesB)) else do let moveA = echo movesB let moveB = convertMove ch putStrLn ("nA plays: " ++ show moveA ++ ", B plays: " ++ show moveB) playInteractive (moveA : movesA, moveB : movesB)

Rock - Paper - Scissors write a function that will let the user play the game interactively let the computer play the“echo”strategy playInteractive :: Match -> IO () playInteractive (movesA, movesB) = do ch <- getChar if not (ch `elem` "rpsRPS") then putStrLn (showResult (movesA, movesB)) else do let moveA = echo movesB let moveB = convertMove ch putStrLn ("nA plays: " ++ show moveA ++ ", B plays: " ++ show moveB) playInteractive (moveA : movesA, moveB : movesB)

Rock - Paper - Scissors write a function to represent the outcome of a match: showResult :: Match -> String write a function to convert a character into a move: convertMove :: Char -> Move

Rock - Paper - Scissors write a function that will let computer play the“cycle”strategy versus the“echo”strategy for a number of times generateMatchCycleVsEcho :: Integer -> Match generateMatchCycleVsEcho 0 = ([], []) generateMatchCycleVsEcho n = step (generateMatchCycleVsEcho (n - 1)) where step :: Match -> Match step (movesA, movesB) = (cycle’ movesB : movesA, echo movesA : movesB)

Rock - Paper - Scissors write a function that will let computer play the“cycle”strategy versus the“echo”strategy for a number of times generateMatchCycleVsEcho :: Integer -> Match generateMatchCycleVsEcho 0 = ([], []) generateMatchCycleVsEcho n = step (generateMatchCycleVsEcho (n - 1)) where step :: Match -> Match step (movesA, movesB) = (cycle’ movesB : movesA, echo movesA : movesB)

Rock - Paper - Scissors write a function to represent all moves in a match: showMoves :: Match -> String write a function to represent the moves and result of a match: showMatch :: Match -> String

References Required Reading: Thompson Chapter 8: Playing the game: I/O in Haskell

Add a comment

Related presentations

Related pages

Functional Programming Input/Output Distinguished ...

Andrew D. Gordon - Functional Programming Input/Output (Distinguished Dissertations in Computer Science, Band jetzt kaufen. ISBN: 9780521070072 ...
Read more

Functional Programming and Input/Output (Distinguished ...

Buy Functional Programming and Input/Output (Distinguished Dissertations in Computer Science) on Amazon.com FREE SHIPPING on qualified orders
Read more

Functional Programming and Input/Output FUNCTIONAL ...

Andrew D. Gordon - Functional Programming and Input/Output[ FUNCTIONAL PROGRAMMING AND jetzt kaufen. Kundrezensionen und 0.0 Sterne. …
Read more

Functional Programming and Input/Output.

Functional Programming and Input/Output. Andrew D. Gordon. Distinguished Dissertations in Computer Science. Cambridge University Press, 1994. ISBN 0 521 ...
Read more

Functional Programming and Input/Output - Google Books

A common attraction to functional programming is the ease with which proofs can be given of program properties. A common disappointment with functional ...
Read more

Functional Programming and Input / Output : Andrew D ...

Functional Programming and Input / Output by Andrew D. Gordon, 9780521070072, available at Book Depository with free delivery worldwide.
Read more

Functional Programming And Input Output

Title: Functional Programming And Input Output Subject: Functional Programming And Input Output Keywords: Download or Read Online functional programming ...
Read more

Functional Programming and Input/Output

978-0-521-07007-2 - Functional Programming and Input/Output Andrew D. Gordon Frontmatter More information. Title: Functional Programming and Input/Output ...
Read more

Functional Programming and Input/Output von Andrew D ...

Große Auswahl fremdsprachige Bücher bei Thalia Bücher versandkostenfrei »Functional Programming and Input/Output« jetzt bestellen!
Read more

Input-Output - CS190 Functional Programming Techniques

Input-Output CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn 2008 – Week 7 Dr Hans Georg Schaathun Input ...
Read more