0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
12 Haskell.  ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip ftp://web.ntnu.edu.tw/WWW/func_prog/ghc zip  Haskell is  Lazy evaluated  Case-sensitive.
Chapter 16 Communicating With the Outside World. Motivation  In Chapter 3 we learned how to do basic IO, but it was fairly limited.  In this chapter.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Top-Down Design CSC 161: The Art of Programming Prof. Henry Kautz 9/16/2009.
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Chapter 3 Simple Graphics. Side Effects and Haskell  All the programs we have seen so far have no “side-effects.” That is, programs are executed only.
Character Input and Output C and Data Structures Baojian Hua
Chapter 2: Algorithm Discovery and Design
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Computer Science 1620 Programming & Problem Solving.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
0 PROGRAMMING IN HASKELL Chapter 3 - Types and Classes.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
Character Input and Output
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
Chapter 2: Algorithm Discovery and Design
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
C - Input & Output When we are saying Input that means to feed some data into program. This can be given in the form of file or from command line. C programming.
Fundamentals of Python: From First Programs Through Data Structures
1.
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
Haskell Chapter 8. Input and Output  What are I/O actions?  How do I/O actions enable us to do I/O?  When are I/O actions actually performed?
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 5 – Dental Payment Application: Introducing.
0 PROGRAMMING IN HASKELL Some first steps Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
What is a Type? A type is a name for a collection of related values. For example, in Haskell the basic type Bool contains the two logical values: True.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
1 Program Input Software Design Chapter 4. 2 You Will Want to Know... Prompting for and reading values into a program. Accessing data from a file. What.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
1 CS161 Introduction to Computer Science Topic #8.
Functional Programming Lecture 3 - Lists Muffy Calder.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
CCSA 221 Programming in C INPUT AND OUTPUT OPERATIONS IN C – PART 1 1.
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Haskell Chapter 8.
What is a Parser? A parser is a program that analyses a piece of text to determine its syntactic structure  3 means 23+4.
PROGRAMMING IN HASKELL
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
A lightening tour in 45 minutes
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 30-Nov-18.
Chapter 3: Input/Output
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Programming Languages
PROGRAMMING IN HASKELL
Computer Science 312 I/O and Side Effects 1.
PROGRAMMING IN HASKELL
Theory of Computation Lecture 4: Programs and Computable Functions II
Haskell Dealing with impurity 29-May-19.
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs

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. batch program inputsoutputs

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. interactive program inputsoutputs keyboard screen

3 The Problem Haskell programs are pure mathematical functions: However, reading from the keyboard and writing to the screen are side effects: zHaskell programs have no side effects. zInteractive 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 zThe action getChar reads a character from the keyboard, echoes it to the screen, and returns the character as its result value:

7 zThe action putChar c writes the character c to the screen, and returns no result value: putChar :: Char  IO () zThe 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 a :: IO (Char,Char) a = 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) zReading a string from the keyboard:

10 putStr :: String  IO () putStr [] = return () putStr (x:xs) = do putChar x putStr xs zWriting a string to the screen: zWriting 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: abcde The string has 5 characters zEvaluating an action executes its side effects, with the final result value being discarded. Note:

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

14 zThe game ends when the guess is correct. hangman :: IO () hangman = do putStrLn "Think of a word: " word  sgetLine putStrLn "Try to guess it:" guess 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 c  getChar hSetEcho stdin True return c The action getCh reads a single character from the keyboard, without echoing it to the screen:

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

18 The function diff indicates which characters in one string occur in a second string: For example: > diff "haskell" "pascal" "-as--ll" diff :: String  String  String diff 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: zThe board comprises five rows of stars: 1: * * * * * 2: * * * * 3: * * * 4: * * 5: *

20 zTwo players take it turn about to remove one or more stars from the end of a single row. zThe 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].