Functional Programming - Input/Output
Upcoming SlideShare
Loading in...5
×
 

Functional Programming - Input/Output

on

  • 169 views

I/O in Haskell.

I/O in Haskell.

Statistics

Views

Total Views
169
Views on SlideShare
169
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Functional Programming - Input/Output Functional Programming - Input/Output Presentation Transcript

  • 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 Introduction String Conversions Action Sequences 2 Example: Rock-Paper-Scissors Data Types Strategies Game Play
  • Topics 1 I/O Model Introduction 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 I/O doesn’t produce a result: 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 Introduction 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 Introduction 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 produced by the program: <- 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 the action sequence 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 a number of 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 Introduction 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])
  • Outcome determine the outcome of one round player A wins → 1, player 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
  • String Conversions moves in a round as a string showRound :: (Move,Move) -> String showRound (moveA,moveB) = "A plays: " ++ show moveA ++ ", B plays: " ++ show moveB exercise: match result as a string showResult [Rock,Paper] [Paper,Scissors] "Player B wins by 2" showResult [Rock,Paper] [Paper,Rock] "It’s a tie"
  • Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock-Paper-Scissors Data Types Strategies Game Play
  • 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
  • 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
  • Strategies cycle through the possibilities cycle :: [Move] -> Move cycle moves = case (length moves) `mod` 3 of 0 -> Rock 1 -> Paper 2 -> Scissors
  • Strategies play whatever the opponent played last -- opponent moves kept in reverse order echo :: [Move] -> Move echo [] = Rock echo (latest:rest) = latest
  • Topics 1 I/O Model Introduction String Conversions Action Sequences 2 Example: Rock-Paper-Scissors Data Types Strategies Game Play
  • 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"
  • Game Play computer plays 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 ([],[])
  • Automatic Play 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)
  • References Required Reading: Thompson Chapter 8: Playing the game: I/O in Haskell