Haskell Chapter 9. More Input and More Output  Files and Streams  Transforming Input  Not covered  brackets  command-line arguments  bytestrings.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 7 Semantics Surely all this is not without.
Functional Programming Lecture 10 - type checking.
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.
Kathleen Fisher cs242 Reading: Tackling the Awkward Squad, Sections 1-2Tackling the Awkward Squad Real World Haskell, Chapter 7: I/OReal World Haskell.
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Internship in DASAN networks C programing language Chapter 7 : Input and Output Present by Le Thi Hien 1/14.
Formal Language, chapter 4, slide 1Copyright © 2007 by Adam Webber Chapter Four: DFA Applications.
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.
Recursive Data Types Koen Lindström Claessen. Modelling Arithmetic Expressions Imagine a program to help school-children learn arithmetic, which presents.
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.
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.
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs.
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 Haskell: Higher-order Functions Dr. Hyunyoung Lee.
WEEK 6 Class Activities Lecturer’s slides.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Python – May 12 Recap lab Chapter 2 –operators –Strings –Lists –Control structures.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
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.
Recursion Higher Order Functions CSCE 314 Spring 2016.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
Dictionaries and File I/O George Mason University.
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.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 7 Strings Chapter.
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
Types CSCE 314 Spring 2016.
dr Robert Kowalczyk WMiI UŁ
PROGRAMMING IN HASKELL
Haskell Chapter 9.
PROGRAMMING IN HASKELL
Higher-Order Functions
PROGRAMMING IN HASKELL
Register Use Policy Conventions
Main Points of Haskell Functional programming Laziness
CSE 341 : Programming Languages Section 5 Haskell: Round 3
Haskell Dealing with impurity 30-Nov-18.
Haskell Dealing with impurity 30-Nov-18.
BTEC COMPUTING – UNIT 1 SECTION B - FLOWCHARTS
Input and Output.
Higher Order Functions
“Shell Scripting” with SML
ECE 103 Engineering Programming Chapter 51 Random Numbers
Haskell Dealing with impurity 8-Apr-19.
PROGRAMMING IN HASKELL
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
Haskell Dealing with impurity 29-May-19.
PROGRAMMING IN HASKELL
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

Haskell Chapter 9

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

Transforming input  Common pattern: get string from input, transform, output result  Use interact main = interact shortLinesOnly shortLinesOnly :: String -> String shortLinesOnly = unlines. filter (\line -> length line < 10). lines Quick Ex: Look up lines, unlines

Another example respondPalindromes :: String -> String respondPalindromes = unlines. map (\xs -> if isPal xs then "palindrome" else "not a palindrome"). lines isPal :: String -> Bool isPal xs = xs == reverse xs main2 = interact respondPalindromes

Reading a file 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()

Randomness  Referential transparency: function given the same parameters twice must return same result  SO, we bring in randomness from outside (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)