Haskell Chapter 8.

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
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
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.
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.
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?
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
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.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L13-1 October 26, 2006http:// Monadic Programming October.
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.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
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.
Haskell Chapter 9. More Input and More Output  Files and Streams  Transforming Input  Not covered  brackets  command-line arguments  bytestrings.
Today… Style, Cont. – Naming Things! Methods and Functions Aside - Python Help System Punctuation Winter 2016CISC101 - Prof. McLeod1.
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.
JavaScript Part 1 Introduction to scripting The ‘alert’ function.
Advanced Functional Programming 2010
Chapter 2 Writing Simple Programs
Polymorphic Functions
More about comments Review Single Line Comments The # sign is for comments. A comment is a line of text that Python won’t try to run as code. Its just.
Haskell: Syntax in Functions
Types CSCE 314 Spring 2016.
Introduction to Python
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Haskell Chapter 1, Part I.
CS 4450: Principles of Programming Languages
Haskell.
PROGRAMMING IN HASKELL
Main Points of Haskell Functional programming Laziness
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Conditions and Ifs BIS1523 – Lecture 8.
Haskell Dealing with impurity 30-Nov-18.
Haskell Dealing with impurity 30-Nov-18.
T. Jumana Abu Shmais – AOU - Riyadh
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Type & Typeclass Syntax in function
Testing and Repetition
Python programming exercise
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) Aka idempotency in other contexts (e.g., get verb in web programming). 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 () 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) also yields a result

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

What is getLine? getLine is an IO action that yields a String getLine is impure *Main> :t getLine getLine :: IO String

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 () * monad

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 main4 = do line <- getLine if null line 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 Try commenting out putStrLn line. What happens then?

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 Quick trace: what is this doing?

When to use return May use when we need an IO action that doesn’t do anything (like the return () in main4) 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 main11 = do putStr "Enter your name: " name <- getLine putStrLn $ "Hello " ++ name return True

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 () -- must always have else main8 = do 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]

Other useful functions in book forever forM

Play and Share? None for this chapter