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?

Slides:



Advertisements
Similar presentations
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Advertisements

Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
CS 330 Programming Languages 10 / 14 / 2008 Instructor: Michael Eckmann.
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.
CS 330 Programming Languages 10 / 11 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 01 / 29 / 2008 Instructor: Michael Eckmann.
Cse536 Functional Programming 1 6/23/2015 Lecture #17, Dec. 1, 2004 Todays Topics – Higher Order types »Type constructors that take types as arguments.
Chapter 2 Writing Simple Programs
0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs.
C Programming. Chapter – 1 Introduction Study Book for one month – 25% Learning rate Use Compiler for one month – 60%
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
Hello AP Computer Science!. What are some of the things that you have used computers for?
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Introduction to Computational Linguistics Programming I.
Haskell. 2 GHC and HUGS Haskell 98 is the current version of Haskell GHC (Glasgow Haskell Compiler, version 7.4.1) is the version of Haskell I am using.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
CS 106 Introduction to Computer Science I 01 / 31 / 2007 Instructor: Michael Eckmann.
0 Functors in Haskell Adapted from material by Miran Lipovaca.
© 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.
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
1 Printing in Python Every program needs to do some output This is usually to the screen (shell window) Later we’ll see graphics windows and external files.
Monads. foo1 Method to print a string, then return its length: scala> def foo1(bar: String) = { | println(bar) | bar.size | } foo1: (bar: String)Int scala>
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
FUNCTIONAL PROGRAMING AT WORK - HASKELL AND DOMAIN SPECIFIC LANGUAGES Dr. John Peterson Western State Colorado University.
Haskell Chapter 9. More Input and More Output  Files and Streams  Transforming Input  Not covered  brackets  command-line arguments  bytestrings.
Program Development Cycle 1.Edit program 2.Compile program - translates it from C to machine language 3. Run/execute your program. 4. If not satisfied,
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
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.
0 PROGRAMMING IN HASKELL Typeclasses and higher order functions Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and.
Advanced Functional Programming 2010
Chapter 2 Writing Simple Programs
Polymorphic Functions
Haskell Chapter 8.
Haskell: Syntax in Functions
Types CSCE 314 Spring 2016.
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
CS 4450: Principles of Programming Languages
Haskell Chapter 9.
Haskell.
PROGRAMMING IN HASKELL
Main Points of Haskell Functional programming Laziness
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 30-Nov-18.
Haskell Dealing with impurity 30-Nov-18.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
Input and Output.
Fundamentals of Functional Programming
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Programming Languages
Computer Science 312 I/O and Side Effects 1.
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.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Presentation transcript:

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?

Review of Functional Language characteristics  Haskell is purely functional  Provide definitions of what things are, Haskell figures out the steps to execute  Functions are not allowed to have side effects  If a function is called two times with the same parameters, it must return the same result (referential transparency)  Helps us reason about our programs (e.g., don’t have to trace entire program to see if some other function is messing with your variables!)

I/O  But what about showing the result? That changes the state of the screen… a side effect!  What to do???  Separate the “impure” parts of the program from the “pure” parts. Like FP on steroids

Our first program main = putStrLn "Hello World!" At the command prompt  ghc --make helloworld  helloworld (windows) or./helloworld (nix)

What’s putStrLn?  Prelude> :t putStrLn  putStrLn :: String -> IO ()  Prelude> :t putStrLn "hello, world"  putStrLn "hello, world" :: IO ()  putStr take a String, returns an IO action  An IO action does some action with a side effect (e.g., read input or write output) and also yields a result  The value of putStrLn is just ()

How does this work in GHCi?  *Main> putStrLn "howdy pardner"  howdy pardner  In GHCi, it performs the IO action and then prints it to the terminal using putStrLn

When are I/O actions performed?  Must give it a name (e.g., main) and then run the program  Can glue several I/O actions into one.  Don’t usually specify a type declaration for main – it has signature of main :: IO something where something is a concrete type, for example:  *Main> :t main  main :: IO ()  getLine is an IO action that yields a String  getLine is impure  *Main> :t getLine  getLine :: IO String

So what exactly is an IO action??  You can think of it as a box with feet.  Goes out into the world and does something  Maybe brings something back  Use <- to “open” the box and get the value  We can only take it out when we’re inside another IO action  This won’t work!!  nameTag = "Hello, my name is " ++ getLine

How do the pure and impure parts interact? main2 = do putStrLn "Hello, what's your name?" name <- getLine putStrLn $ "Zis is your future: " ++ tellFortune name tellFortune name | name == "Lucky" = name ++ ",you will win a million dollars" | otherwise = name ++ ", you will have no money but be happy"  *Main> main2  Hello, what's your name?  Lucky  Zis is your future: Lucky,you will win a million dollars  tellFortune is just a regular function!  *Main> tellFortune "Cyndi"  "Cyndi, you will have no money but be happy"

Bindings import Data.Char main3 = do putStrLn "What's your first name? " firstName <- getLine putStrLn "What's your last name? " lastName <- getLine let bigFirstName = map toUpper firstName let bigLastName = map toUpper lastName putStrLn $ "hey " ++ bigFirstName ++ " " ++ bigLastName ++ ", how are you?“  <- binds values from IO actions to names  let binds pure expressions to names

More details - return  return() is not like other languages.   return makes an I/O action out of a pure value (i.e., takes a value and “wraps” it in an IO action box). Return does not end execution. main4 = do line <- getLine if null line then return () else do putStrLn $ reverseWords line main4 reverseWords :: String -> String reverseWords = unwords. map reverse. words remember: if must always have else

Understanding return main5 = do return () return "HAHAHA" line <- getLine return "BLAH BLAH" return 4 putStrLn line  Creates lots of I/O actions that are not used

More on return main6 = do a <- return "Hello" b <- return "World" putStrLn $ a ++ " " ++ b  More likely to use let in this scenario… but it illustrates how return works  Common to use return when last statement of do doesn’t have desired result. So create a return that yields the desired result, put it at the end  Also used when we need an IO action that doesn’t do anything (like the return () in main4) Quick trace: what is this doing?

Other useful functions  putStr – like putStrLn, but no newline  putChar – takes a character and returns an IO action that will print it to the terminal - putChar 'a'  print – takes a value that’s an instance of Show (i.e., we know how to represent as string), applies show to stringify it, outputs string to terminal - print "haha"

when  when – syntactic sugar. Takes Bool & I/O, returns I/O  when :: Monad m => Bool -> m () -> m () import Control.Monad main7 = do input <- getLine if (input == "SWORDFISH") then putStrLn input else return () main8 = do input <- getLine when (input == "SWORDFISH") $ do putStrLn input

sequence  sequence takes a list of I/O actions, returns an I/O action that performs those actions one after the other main9 = do a <- getLine b <- getLine c <- getLine print [a,b,c] main10 = do rs <- sequence [getLine, getLine, getLine] print rs

mapM  Common scenario: map a function that returns an I/O action (e.g., print) over a list and then sequence it. mapM does this for us, mapM_ does it but throws away the result.  *Main> mapM print [1,2,3]  1  2  3  [(),(),()]  *Main> mapM_ print [1,2,3]  1  2  3

Other useful functions in book  forever  forM

Play and Share?  Let’s start on the homework