Computer Science 312 I/O and Side Effects 1.

Slides:



Advertisements
Similar presentations
Getting Input in Python Assumes assignment statement, typecasts.
Advertisements

Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
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 Writing Simple Programs
0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs.
Lesson 7: Improving the User Interface
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Computer Science 111 Fundamentals of Programming I Basic Program Elements.
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?
Input, Output, and Processing
Computer Science 101 Introduction to Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© 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.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
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.
Variables and Assignment CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
Exercise 1 #include int main() { printf(“Hello C Programming!\n”); return 0; } 1.Run your Visual Studio 2008 or Create a new “project” and add.
Introduction to Programming
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.
Introduction to Programming
Chapter 2 Writing Simple Programs
Haskell Chapter 8.
Fundamentals of Programming I Overview of Programming
Topics Designing a Program Input, Processing, and Output
Python: Experiencing IDLE, writing simple programs
Theory of Computation Lecture 4: Programs and Computable Functions II
Chapter 3 Assignment and Interactive Input.
PROGRAMMING IN HASKELL
Data Types and Conversions, Input from the Keyboard
Variables, Expressions, and IO
Introduction to Programming
INPUT STATEMENTS GC 201.
PROGRAMMING IN HASKELL
Python Primer 2: Functions and Control Flow
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
Topics Introduction to File Input and Output
Functions, Procedures, and Abstraction
Haskell Dealing with impurity 30-Nov-18.
Reading Input from the Keyboard
Chapter 3 Input output.
Rocky K. C. Chang September 18, 2018 (Based on Zelle and Dierbach)
Introduction to Programming
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
Topics Designing a Program Input, Processing, and Output
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
Topics Designing a Program Input, Processing, and Output
Terminal-Based Programs
Programming Languages
Introduction to Programming
Chopin’s Nocturne.
Functional Programming and Haskell
Theory of Computation Lecture 4: Programs and Computable Functions II
Text Files and Random Numbers
Haskell Dealing with impurity 29-May-19.
Topics Introduction to File Input and Output
Haskell Dealing with impurity 28-Jun-19.
Functions, Procedures, and Abstraction
Data Types and Expressions
Advanced Functional Programming
Functional Programming and Haskell
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

Computer Science 312 I/O and Side Effects 1

Functional “Programming” Structure code in terms of cooperating functions and data types Computation is a transformation of values into other values This occurs via the evaluation of expressions, among which are function applications It’s a pure universe: nothing ever really changes, no side effects

Where Is the Program? Pure function applications are not really programs A real program takes input from the outside world, processes it, and sends output back to the outside world The parts of a program that simply perform computations on data might be pure functions, but the parts that deal with the outside world, or with changes of state more generally, cannot be pure in that sense

Isolate the Pure from the Impure External world (files, I/O devices, network ports, etc.) Impure Impure code Pure code

Example: Output with putStrLn Like Python’s print: Prelude> putStrLn “2 more years of Trump!" 2 more years of Trump! Prelude> :type putStrLn putStrLn :: String -> IO () Takes a String as an argument, prints it, and returns nothing! IO is a type class, and () is the empty type Run only for its side effect, producing output on the terminal

Example: Input with getLine Like Python’s input: Prelude> getLine -- Input is in italic on next line Some input text "Some input text" Prelude> :type getLine getLine :: IO String Takes no arguments, waits for input, and returns the string that the user enters at the keyboard IO is a type class for types associated with I/O functions Different calls can produce different results (not pure!)

Actions as Impure Functions putStrLn and getLine express actions, which produce side effects They are impure, in that they can produce different values with the same arguments

Extracting a Value from an Action Prelude> name <- getLine -- Input is in italic on next line Ken Prelude> putStrLn name ++ " Lambert" Ken Lambert Use the <- operator, not the = operator, to extract a value from an action and bind a variable to it

A Standalone Program Defines a function main IO () Runs a sequence of actions in a do block These actions can take inputs, process them, and output results

Example: Entering One’s Name -- SimpleIO.hs module Main (main) where main :: IO () main = do putStr "Enter your first name: " firstName <- getLine putStr "Enter your last name: " lastName <- getLine let fullName = firstName ++ " " ++ lastName putStrLn ("Your full name is " ++ fullName) A do block allows you to run a sequence of actions and pure function applications, such as let

Run in the GHCI to Test Prelude> :load SimpleIO [1 of 1] Compiling Main Ok, one module loaded. *Main> main Enter your first name: Ken Enter your last name: Lambert Your full name is Ken Lambert

Compile Standalone to Deploy $ ghc --make SimpleIO [1 of 1] Compiling Main ( SimpleIO.hs, SimpleIO.o ) Linking SimpleIO ... $ ./SimpleIO Ken Lambert Enter your first name: Enter your last name: Your full name is Ken Lambert The prompts and inputs appear to be out of sync The terminal waits for a newline before displaying output, but the putStr function has not provided it; only the final call of putStrnLn does that

Remedy: Force Outputs with Newlines -- SimpleIO.hs module Main (main) where main :: IO () main = do putStr "Enter your first name: " hFlush stdout firstName <- getLine putStr "Enter your last name: " lastName <- getLine let fullName = firstName ++ " " ++ lastName putStrLn ("Your full name is " ++ fullName) hFlush flushes the output buffer, forcing text to the terminal

Compile Standalone to Deploy $ ghc --make SimpleIO [1 of 1] Compiling Main ( SimpleIO.hs, SimpleIO.o ) Linking SimpleIO ... $ ./SimpleIO Enter your first name: Ken Enter your last name: Lambert Your full name is Ken Lambert

Package I/O Details in a Function module terminalIO (getString) where getString :: String -> IO String -- Note IO String here getString prompt = do putStr prompt hFlush stdout inputString <- getLine return inputString Like Python’s input function, getString displays the prompt and waits for input The <- operator extracts the input string from the action in getLine The return function makes this value available to be extracted by the caller of getString

A Cleaner, Simpler I/O App -- SimpleIO.hs module Main (main) where import TerminalIO (getString) main :: IO () main = do firstName <- getString "Enter your first name: " lastName <- getString "Enter your last name: " let fullName = firstName ++ " " ++ lastName putStrLn ("Your full name is " ++ fullName) The do block contains impure code for I/O and pure code (the let) for processing

Mixing the Pure and the Impure Impure functions (those that produce side effects) cannot be called from within pure functions Pure functions can be called within impure functions Therefore, any function that calls an impure function must also be impure, and its signature must specify this

What about Numeric Inputs? Python: >>> salary = float(input("Enter your salary: ")) Enter your salary: 42000.55 >>> salary 42000.55 Haskell: Prelude> salaryString <- getString "Enter your salary: " Enter your salary: 42000.55 Prelude> salary = read salaryString :: Float Prelude> salary 42000.55

Package I/O Details in a Function module terminalIO (getString, getFloat) where getString :: String -> IO String -- Note IO String here getString prompt = do putStr prompt hFlush stdout inputString <- getLine return inputString getFloat :: String -> IO Float -- Note IO Float here getFloat prompt = do inputString <- getString prompt return (read inputString :: Float)

A Simple Tax Calculator {-File: TaxCode.hs-} module Main (main) where import TerminalIO (getInt, getFloat) main :: IO () main = do income <- getFloat "Enter your income: " exemptionAmount <- getFloat "Enter the exemption amount: " exemptions <- getInt "Enter the number of exemptions: " taxRate <- getInt "Enter your tax rate as a percent: " let tax = income * fromIntegral taxRate / 100.0 - exemptionAmount * exemptions putStrLn ("Your tax is " ++ show tax))

Model/View Pattern Separate code that manages (transforms) the data from code that manages the I/O The model’s code is, for the most part, pure The view’s code is, for the most part, impure Factor these two areas of concern into different modules

Temperature Conversion: The Model {- File: Conversions.hs Purpose: some conversion functions -} module Conversions (celsiusToFahr, fahrToCelsius) where -- Temperature conversions celsiusToFahr :: Float -> Float celsiusToFahr degreesC = degreesC * 9 / 5 + 32 fahrToCelsius :: Float -> Float fahrToCelsius degreesF = (degreesF - 32) * 5 / 9 Pure code!

Temperature Conversion: The View {- File: Conversions.hs Purpose: some conversion functions -} module Main (main) where import Conversions (fahrToCelsius) import TerminalIO (getFloat, getString) main :: IO () main = do degreesF <- getFloat "Enter the degrees Fahrenheit: " let degreesC = fahrToCelsius degreesF putStr "The degrees Celsius is " putStrLn (show degreesC) Impure code!

Iterative Interaction {- File: Conversions.hs Purpose: some conversion functions -} module Main (main) where main :: IO () main = do degreesF <- getString ("Enter the degrees Fahrenheit " ++ "or return to quit: ") if degreesF == "" then return () else do let degreesC = fahrToCelsius (read degreesF :: Float) putStr "The degrees Celsius is " putStrLn (show degreesC) main

Summary I/O functions are impure, in that they produce side effects Calls of pure functions can be embedded in impure functions, but not conversely Impure functions contain sequences of actions (in the imperative style) Values are extracted from actions with <-, and are made available for extraction with return

Working with files Random numbers for non-determinism For next time Working with files Random numbers for non-determinism