PROGRAMMING IN HASKELL

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

Functional Programming Lecture 10 - type checking.
How SAS implements structured programming constructs
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.
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.
Chapter 2: Algorithm Discovery and Design
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
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
0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs.
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.
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?
© 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.
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.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
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.
Conditional Expressions
PROGRAMMING IN HASKELL
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Programming Fundamental
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
Chapter 4: Making Decisions.
Formatted and Unformatted Input/Output Functions
Looping.
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
PROGRAMMING IN HASKELL
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Grab Bag of Interesting Stuff
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.
Lecture 7 – Unit 1 – Chatbots Python – For loops + Robustness
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

PROGRAMMING IN HASKELL Chapter 10 - Interactive Programming

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

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 inputs outputs keyboard screen

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

The type of actions that return a value of type a. 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.

The type of actions that return a character. 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: () is the type of tuples with no components.

Basic Actions The standard library provides a number of actions, including the following three primitives: 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

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

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)

Derived Primitives 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)

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

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"

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

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

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

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)

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

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

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"

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

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