Interactive Programming in Haskell: Chapter 10 Overview

0
P
R
O
G
R
A
M
M
I
N
G
 
I
N
 
H
A
S
K
E
L
L
Chapter 10 - Interactive Programming
1
Introduction
To date, we have seen how Haskell can be used to
write 
batch
 programs that take all their inputs at
the start and give all their outputs at the end.
2
However, we would also like to use Haskell to write
interactive
 programs that read from the keyboard
and write to the screen, as they are running.
3
The Problem
Haskell programs are pure mathematical functions:
However, reading from the keyboard and writing
to the screen are side effects:
z
Haskell programs 
have no side effects
.
z
Interactive programs 
have side effects
.
4
The Solution
Interactive programs can be written in Haskell by
using types to distinguish pure expressions from
impure 
actions
 that may involve side effects.
IO a
The type of actions that
return a value of type a.
5
For example:
IO Char
IO ()
The type of actions that
return a character.
The type of purely side
effecting actions that
return 
no
 result value.
z
() is the type of tuples with no components.
Note:
6
Basic Actions
The standard library provides a number of actions,
including the following three primitives:
getChar :: IO Char
z
The action 
getChar
 reads a character from the
keyboard, echoes it to the screen, and returns
the character as its result value:
7
z
The action 
putChar c
 writes the character c to
the screen, and returns no result value:
putChar :: Char 
 IO ()
z
The action 
return v
 simply returns the value v,
without performing any interaction:
return :: a 
 IO a
8
A sequence of actions can be combined as a single
composite action using the keyword 
do
.
For example:
Sequencing
act :: IO (Char,Char)
act = do x 
 getChar
         getChar
         y 
 getChar
         return (x,y)
9
Derived Primitives
getLine :: IO String
getLine = do x 
 getChar
             if x == '\n' then
                return []
             else
                do xs 
 getLine
                   return (x:xs)
z
Reading a string from the keyboard:
10
putStr :: String 
 IO ()
putStr []     = return ()
putStr (x:xs) = do putChar x
                   putStr xs
z
Writing a string to the screen:
z
Writing a string and moving to a new line:
putStrLn :: String 
 IO ()
putStrLn xs = do putStr xs
                 putChar '\n'
11
Example
We can now define an action that prompts for a
string to be entered and displays its length:
strlen :: IO ()
strlen = do putStr "Enter a string: "
            xs 
 getLine
            putStr "The string has "
            putStr (show (length xs))
            putStrLn " characters"
12
For example:
> strlen
Enter a string: Haskell
The string has 7 characters
z
Evaluating an action 
executes
 its side effects,
with the final result value being discarded.
Note:
13
Hangman
Consider the following version of 
hangman
:
z
One player secretly types in a word.
z
The other player tries to deduce the word, by
entering a sequence of guesses.
z
For each guess, the computer indicates which
letters in the secret word occur in the guess.
14
z
The game ends when the guess is correct.
hangman :: IO ()
hangman = do putStrLn "Think of a word: "
             word 
 sgetLine
             putStrLn "Try to guess it:"
             play word
We adopt a 
top down
 approach to implementing
hangman in Haskell, starting as follows:
15
The action 
sgetLine
 reads a line of text from the
keyboard, echoing each character as a dash:
sgetLine :: IO String
sgetLine = do x 
 getCh
              if x == '\n' then
                 do putChar x
                    return []
              else
                 do putChar '-'
                    xs 
 sgetLine
                    return (x:xs)
16
import System.IO    
getCh :: IO Char  
getCh = do hSetEcho stdin False               
           x 
 getChar              
           hSetEcho stdin True              
           return x
 
The action 
getCh
 reads a single character from the
keyboard, without echoing it to the screen:
17
The function 
play
 is the main loop, which requests
and processes guesses until the game ends.
play :: String 
 IO ()
play word =
   do putStr "? "
      guess 
 getLine
      if guess == word then
         putStrLn "You got it!"
      else
         do putStrLn (match word guess)
            play word
18
The function 
match
 indicates which characters in
one string occur in a second string:
For example:
> match "haskell" "pascal"
  
"-as--ll"
match :: String 
 String 
 String
match xs ys =
   [if elem x ys then x else '-' | x 
 xs]
19
Exercise
Implement the game of 
nim
 in Haskell, where the
rules of the game are as follows:
z
The board comprises five rows of stars:
1: * * * * *
2: * * * *
3: * * *
4: * *
5: *
20
z
Two players take it turn about to remove one
or more stars from the end of a single row.
z
The winner is the player who removes the last
star or stars from the board.
Hint:
Represent the board as a list of five integers that
give the number of stars remaining on each row.
For example, the initial board is [5,4,3,2,1].
Slide Note
Embed
Share

Haskell programs are inherently pure functions without side effects. However, to create interactive programs in Haskell, we use types to distinguish between pure expressions and impure actions involving side effects. Actions like getChar for reading from the keyboard and putChar for writing to the screen are examples of side-effecting actions in Haskell.

  • Haskell
  • Interactive Programming
  • Pure Functions
  • Side Effects

Uploaded on Dec 13, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. PROGRAMMING IN HASKELL PROGRAMMING IN HASKELL Chapter 10 - Interactive Programming 0

  2. Introduction To date, we have seen how Haskell can be used to write batch programs that take all their inputs at the start and give all their outputs at the end. batch program inputs outputs 1

  3. However, we would also like to use Haskell to write interactive programs that read from the keyboard and write to the screen, as they are running. keyboard interactive program inputs outputs screen 2

  4. The Problem Haskell programs are pure mathematical functions: z Haskell programs have no side effects. However, reading from the keyboard and writing to the screen are side effects: z Interactive programs have side effects. 3

  5. The Solution Interactive programs can be written in Haskell by using types to distinguish pure expressions from impure actions that may involve side effects. IO a The type of actions that return a value of type a. 4

  6. For example: The type of actions that return a character. IO Char The type of purely side effecting actions that return no result value. IO () Note: z () is the type of tuples with no components. 5

  7. Basic Actions The standard library provides a number of actions, including the following three primitives: z The action getChar reads a character from the keyboard, echoes it to the screen, and returns the character as its result value: getChar :: IO Char 6

  8. z The action putChar c writes the character c to the screen, and returns no result value: putChar :: Char IO () z The action return v simply returns the value v, without performing any interaction: return :: a IO a 7

  9. Sequencing A sequence of actions can be combined as a single composite action using the keyword do. For example: act :: IO (Char,Char) act = do x getChar getChar y getChar return (x,y) 8

  10. Derived Primitives z Reading a string from the keyboard: getLine :: IO String getLine = do x getChar if x == '\n' then return [] else do xs getLine return (x:xs) 9

  11. z Writing a string to the screen: putStr :: String IO () putStr [] = return () putStr (x:xs) = do putChar x putStr xs z Writing a string and moving to a new line: putStrLn :: String IO () putStrLn xs = do putStr xs putChar '\n' 10

  12. Example We can now define an action that prompts for a string to be entered and displays its length: strlen :: IO () strlen = do putStr "Enter a string: " xs getLine putStr "The string has " putStr (show (length xs)) putStrLn " characters" 11

  13. For example: > strlen Enter a string: Haskell The string has 7 characters Note: z Evaluating an action executes its side effects, with the final result value being discarded. 12

  14. Hangman Consider the following version of hangman: z One player secretly types in a word. z The other player tries to deduce the word, by entering a sequence of guesses. z For each guess, the computer indicates which letters in the secret word occur in the guess. 13

  15. z The game ends when the guess is correct. We adopt a top down approach to implementing hangman in Haskell, starting as follows: hangman :: IO () hangman = do putStrLn "Think of a word: " word sgetLine putStrLn "Try to guess it:" play word 14

  16. The action sgetLine reads a line of text from the keyboard, echoing each character as a dash: sgetLine :: IO String sgetLine = do x getCh if x == '\n' then do putChar x return [] else do putChar '-' xs sgetLine return (x:xs) 15

  17. The action getCh reads a single character from the keyboard, without echoing it to the screen: import System.IO getCh :: IO Char getCh = do hSetEcho stdin False x getChar hSetEcho stdin True return x 16

  18. The function play is the main loop, which requests and processes guesses until the game ends. play :: String IO () play word = do putStr "? " guess getLine if guess == word then putStrLn "You got it!" else do putStrLn (match word guess) play word 17

  19. The function match indicates which characters in one string occur in a second string: match :: String String String match xs ys = [if elem x ys then x else '-' | x xs] For example: > match "haskell" "pascal" "-as--ll" 18

  20. Exercise Implement the game of nim in Haskell, where the rules of the game are as follows: z The board comprises five rows of stars: 1: * * * * * 2: * * * * 3: * * * 4: * * 5: * 19

  21. z Two players take it turn about to remove one or more stars from the end of a single row. z The winner is the player who removes the last star or stars from the board. Hint: Represent the board as a list of five integers that give the number of stars remaining on each row. For example, the initial board is [5,4,3,2,1]. 20

Related


More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#