Haskell Chapter 9.

Slides:



Advertisements
Similar presentations
Introduction A function is called higher-order if it takes a function as an argument or returns a function as a result. twice :: (a  a)  a  a twice.
Advertisements

Kathleen Fisher cs242 Reading: Tackling the Awkward Squad, Sections 1-2Tackling the Awkward Squad Real World Haskell, Chapter 7: I/OReal World Haskell.
25-Aug-14 Simple Java I/O Part I General Principles.
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.
UNIT 12 UNIX I/O Redirection.
The IO Monad Mooly Sagiv Slides from John Mitchell Kathleen Fisher Simon Peyton Jones Reading: “Tackling the Awkward Squad” “Real World Haskell,” Chapter.
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
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.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
Higher-Order Functions Koen Lindström Claessen. What is a “Higher Order” Function? A function which takes another function as a parameter. Examples map.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 7: Semantics Fall 2009 Marco.
Advanced Programming Handout 12 Higher-Order Types (SOE Chapter 18)
Cse536 Functional Programming 1 6/30/2015 Lecture #16, Nov. 29, 2004 Todays Topics – Files, Channels, and Handles –IO exception handling –First Class Channels.
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
Haskell programming language. Haskell is… Memory managed (allocation, collection) “Typeful” (static, strong) – Types are checked at compile-time – Types.
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?
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
© 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.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Case Study: Route Finding Lecture 6, Programmeringsteknik del A.
Grab Bag of Interesting Stuff. Higher Order types Type constructors are higher order since they take types as input and return types as output. Some type.
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.
Data in Haskell. What is data? Measurements of some kind stored in a computer. – Examples Simple: numbers, text, truth values … Structured: sequences,
Chapter 16 Advanced Bourne Shell Programming. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Objectives To discuss numeric data processing.
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.
Två saker ● Labsal bokning ● Sprid ut er över salarna ● “Bracket Matching” [ " Slides: " ++ concat (intersperse ", " (map lnk gs)) | let gs = [ f | f.
Advanced Functional Programming 2010
Haskell Chapter 8.
Haskell N/W ?? Rashmesh Radhakrishnan
Koen Lindström Claessen
Command Line Arguments
CS1010 Programming Methodology
A lightening tour in 45 minutes
Haskell programming language.
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Main Points of Haskell Functional programming Laziness
PROGRAMMING IN HASKELL
Introduction to javadoc
PROGRAMMING IN HASKELL
CS 2308 Exam I Review.
Chapter 14 - Advanced C Topics
CSE 341 : Programming Languages Section 5 Haskell: Round 3
Haskell Dealing with impurity 30-Nov-18.
Haskell Dealing with impurity 30-Nov-18.
PROGRAMMING IN HASKELL
Input and Output with FILES
Input and Output.
PROGRAMMING IN HASKELL
Fundamentals of Functional Programming
Building Java Programs
Introduction to javadoc
ECE 103 Engineering Programming Chapter 51 Random Numbers
Haskell Dealing with impurity 8-Apr-19.
Grab Bag of Interesting Stuff
PROGRAMMING IN HASKELL
Programming Languages
Computer Science 312 I/O and Side Effects 1.
Why are arrays useful? We can use arrays to store a large amount of data without declaring many variables. Example: Read in a file of 1000 numbers, then.
Text Files and Random Numbers
General Computer Science for Engineers CISC 106 Lecture 03
Haskell Dealing with impurity 29-May-19.
Haskell Dealing with impurity 28-Jun-19.
SPL – PS1 Introduction to C++.
Presentation transcript:

Haskell Chapter 9

More Input and More Output Files and Streams Transforming Input Randomness Not covered brackets command-line arguments bytestrings

Reading a file – brute force import System.IO main3 = do handle <- openFile "haiku.txt" ReadMode contents <- hGetContents handle putStr contents putStr "\n" hClose handle openFile :: FilePath -> IOMode -> IO Handle type FilePath = String data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode

Reading a file – simpler import System.IO main4 = do contents <- readFile "haiku.txt" putStr contents putStr "\n" readFile :: FilePath -> String -> IO() Haiku (from wikipedia) Japanese poetry Essence is “cutting” Traditional has 17 on (syllables) in 3 phrases, with 5 7 5 syllables/phrase

Transforming input Common pattern*: get string from input transform output result lines & unlines – convert between lines separated by \n and array Prelude> lines "aa\nbb\nbb" ["aa","bb","bb"] Prelude> unlines ["the","brown","cow","says","moo"] "the\nbrown\ncow\nsays\nmoo\n" *Haskell has a feature named interact that does this. Doesn’t work well on all machines, so we’re not covering.

More examples words & unwords Prelude> words "Never trust a llama" Prelude> unwords ["the","cat","in","the","hat"] "the cat in the hat“ *Main> unwords $ reverse $ words "this is it" "it is this"

Another example isPal :: String -> Bool isPal xs = xs == reverse xs *Main> map isPal $ words "civic radar banana racecar apple" [True,True,False,True,False] Review: what’s the purpose of $

Randomness Referential transparency: function given the same parameters twice must return same result SO, we bring in randomness from outside (kind of like using Unix time stamp for a seed) Take a random generator, return a random value and new random generator random : : (RandomGen g, Random a) => g -> (a, g) Take an integer, return a random generator mkStdGen :: Int -> StdGen * more random functions in book

Example import System.Random threeCoins :: StdGen -> (Bool, Bool, Bool) threeCoins gen = let (firstCoin, newGen) = random gen (secondCoin, newGen') = random newGen (thirdCoin, newGen'') = random newGen' in (firstCoin, secondCoin, thirdCoin) --threeCoins (mkStdGen 22)