CSCE 314 Programming Languages. Interactive Programming: I/O

Similar documents
PROGRAMMING IN HASKELL. Chapter 10 - Interactive Programming

CSCE 314 Programming Languages. Functional Parsers

References. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs

CSCE 314 Programming Languages. Monadic Parsing

CSCE 314 Programming Languages Functors, Applicatives, and Monads

CSCE 314 Programming Languages

Standard prelude. Appendix A. A.1 Classes

Shell CSCE 314 TAMU. Haskell Functions

CSCE 314 Programming Languages. Monadic Parsing

I/O in Haskell. To output a character: putchar :: Char -> IO () e.g., putchar c. To output a string: putstr :: String -> IO () e.g.

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Introduction to Haskell

IO Monad (3C) Young Won Lim 1/6/18

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

Programming in Haskell Aug Nov 2015

IO Monad (3C) Young Won Lim 8/23/17

CS 457/557: Functional Languages

Haskell Types, Classes, and Functions, Currying, and Polymorphism

IO Monad (3D) Young Won Lim 1/18/18

I/O in Purely Functional Languages. Stream-Based I/O. Continuation-Based I/O. Monads

Programming Languages

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Programming Paradigms

Shell CSCE 314 TAMU. Higher Order Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

CSCE314: Programming Languages Final Examination

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

Haskell Monads CSC 131. Kim Bruce

Shell CSCE 314 TAMU. Functions continued

Monad Background (3A) Young Won Lim 11/18/17

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSCE 314 Programming Languages

Monad Background (3A) Young Won Lim 11/8/17

Using Monads for Input and Output

INTERACTION FUNCTIONS

EECS 700 Functional Programming

Lecture 19: Functions, Types and Data Structures in Haskell

Background Operators (1E) Young Won Lim 7/7/18

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

CS The IO Monad. Slides from John Mitchell, K Fisher, and S. Peyton Jones

CS 11 Haskell track: lecture 1

An introduction introduction to functional functional programming programming using usin Haskell

Functional Programming and Haskell

CS 457/557: Functional Languages

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Background Type Classes (1B) Young Won Lim 6/28/18

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

Haskell: Lists. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, February 24, Glenn G.

M-Structures (Continued)

HASKELL I/O. Curt Clifton Rose-Hulman Institute of Technology. Please SVN Update your HaskellInClass folder, then open eieio.hs

An introduction to functional programming. July 23, 2010

CS 320: Concepts of Programming Languages

Principles of Programming Languages (H)

Haskell Overview II (2A) Young Won Lim 8/9/16

Functional Logic Programming Language Curry

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

Side Effects (3B) Young Won Lim 11/23/17

SML A F unctional Functional Language Language Lecture 19

An Introduction to Programming in Haskell. Gabriel J. Ferrer Hendrix College

Side Effects (3B) Young Won Lim 11/27/17

Principles of Programming Languages, 3

Introduction to Programming, Aug-Dec 2006

Embedded Domain Specific Languages in Idris Lecture 3: State, Side Effects and Resources

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

CIS 252 Introduction to Computer Science Section M013: Exam 3 (Blue) April 26, Points Possible

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Functional Programming in Haskell for A level teachers

CSCE 314 Programming Languages

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

f() ! " Run a higher-priority task? ! " Yield the processor to another task because our ! " Use the processor for some other activity while

CS 410/510: Advanced Programming

Software System Design and Implementation

Programming Language Concepts: Lecture 14

G Programming Languages - Fall 2012

CSCE 314 Programming Languages. Type System

Monads. Functional Programming (CS4011) Monads

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Monad Background (3A) Young Won Lim 10/5/17

Lecture 1 August 9, 2017

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Haskell Revision and Exercises

CSCE 314 Programming Languages

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

Side Effects (3B) Young Won Lim 11/20/17

GHCi: Getting started (1A) Young Won Lim 6/3/17

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Side Effects (3A) Young Won Lim 1/13/18

A Sudoku Solver (1A) Richard Bird Implementation. Young Won Lim 11/15/16

Principles of Programming Languages

Simple design exercise similar to what you will do for your CS102 project (and what happens in industry!) Stages:

Haskell. COS 326 Andrew W. Appel Princeton University. a lazy, purely functional language. slides copyright David Walker and Andrew W.

Transcription:

CSCE 314 Programming Languages Interactive Programming: I/O Dr. Hyunyoung Lee 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 (e.g., a compiler). inputs batch program outputs 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 (e.g., an interpreter). keyboard inputs interactive program outputs screen 3

The Problem: Haskell functions are pure mathematical functions Haskell programs have no side effects. referential transparency: called with the same arguments, a function always returns the same value Lee CSCE 314 TAMU However, reading from the keyboard and writing to the screen are side effects: Interactive programs have side effects. 4

The Solution - The IO Type Interactive programs can be viewed as a pure function whose domain and codomain are the current state of the world: type IO = World -> World However, an interactive program may return a result value in addition to performing side effects: type IO a = World -> (a, World) What if we need an interactive program that takes an argument of type b? b -> IO a b -> World -> (a, World) 5

The Solution (Cont.) Now, interactive programs (impure actions) can be defined using the IO type: IO a For example: IO Char IO () The type of actions that return a value of type a The type of actions that return a character The type of actions that return the empty tuple (a dummy value); purely side-effecting actions 6

Basic Actions (built into the GHC system) 1. 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 2. The action putchar c writes the character c to the screen, and returns no result value: putchar :: Char -> IO () 3. The action return v simply returns the value v, without performing any interaction with the user: return :: a -> IO a 7

Sequencing do notation A sequence of IO actions can be combined into a single composite action using the do notation: The layout rule applies do v1 <- a1 v2 <- a2 a3... vn <- an return (f v1 v2... vn) If the value vi is not used, simply write ai First perform action a1 and call its result value v1,, and finally, apply the function f to combine all the results into a single value, and return it as the result value from the expression as a whole. Called generator because ai generates value for vi 8

Sequencing Example Deafine an action (act1) that reads three characters, discards the second, and returns the first and third as a pair. act1 :: IO (Char,Char) act1 = do x <- getchar getchar y <- getchar return (x,y) The character read by the second getchar is not used 9

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' 10

Building More Complex IO Actions 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: Now, try: > strlen xs <- getline putstr The string has putstr (show (length xs)) putstrln characters. Enter a string: Haskell Rocks! The string has 14 characters. 11

The Type of main A complete Haskell program is a single IO action. For example: main :: IO () main = getline >>= \cs -> putline (reverse cs) Typically, IO contaminates a small part of the program (outermost part), and a larger portion of a Haskell program does not perform any IO. For example, in the above definition of main, reverse is a non-io function. 12

Hangman Consider the following version of hangman: 1. One player secretly types in a word. 2. The other player tries to deduce the word, by entering a sequence of guesses. 3. For each guess, the computer indicates which letters in the secret word occur in the guess. 4. The game ends when the guess is correct. 13

Hangman (Cont.) 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:" guess word 14

Hangman (Cont.) 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 else return [] do putchar '-' xs <- sgetline return (x:xs) 15

Hangman (Cont.) 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 - echo off c <- getchar hsetecho stdin True -- echo on return c 16

Hangman (Cont.) 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 17

Hangman (Cont.) The function diff indicates which characters in one string occur in the second string: diff :: String -> String -> String diff xs ys = [if elem x ys then x else '-' x <- xs] For example: > diff "haskell" "pascal" "-as--ll" 18