Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Input and Output Actions in Haskell - Lecture Slides | CS 457, Study notes of Computer Science

Material Type: Notes; Class: FUNCTIONAL LANGUAGES; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-kmi
koofers-user-kmi 🇺🇸

10 documents

1 / 42

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 457/557: Functional
Languages
I/O Actions in Haskell
Mark P Jones
Portland State University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a

Partial preview of the text

Download Input and Output Actions in Haskell - Lecture Slides | CS 457 and more Study notes Computer Science in PDF only on Docsity!

CS 457/557: Functional

Languages

I/O Actions in Haskell Mark P Jones Portland State University

Question:

If functional programs don’t

have any side-effects, then

how can we ever do anything

useful?

Demo:

… of Mac OS X Automator …

IO Actions:

An IO action is a value of type IO T T is the type of values that it produces

:: IO a

action

The New Haskell Logo:

Building Blocks:

(>>) :: IO a -> IO b -> IO b p >> q is an I/O action in which the output of p is ignored by q p >> q == p >>= \x -> q (where x does not appear in q)

Building Blocks:

inIO :: (a -> b) -> a -> IO b An action inIO f applies the function f to each input of type a and produces outputs of type b as its results

Building Blocks:

mapM :: (a -> IO b) -> [a] -> IO [b] An action mapM f takes a list of inputs of type [a] as its input, runs the action f on each element in turn, and produces a list of outputs of type [b]

Terminal Output:

putStr :: String -> IO () putStrLn :: String -> IO () An action putStr s takes a String input and outputs it on the terminal producing a result of type () putStrLn s does the same thing but adds a trailing new line

Terminal Output:

print :: Show a => a -> IO () A print action takes a value whose type is in Show and outputs a corresponding String on the terminal

Web Actions:

The WebActions module provides the following I/O actions: getText :: URL -> IO String getByteString :: URL -> IO ByteString writeByteString :: String -> ByteString -> IO () downloadTo :: FilePath -> URL -> IO () getTags :: URL -> IO [Tag] getHrefs :: URL -> IO [URL] getHTML :: URL -> IO [TagTree] getXML :: URL -> IO [Content]

Viewing a Webpage:

return url

= getText = putStr

Counting Lines:

return url

= getText = inIO (length. lines) = print

Viewing a Webpage as Tags:

return url

= getTags = inIO (unlines. map show) = putStr