Unformatted text preview:

CSE399: Advanced ProgrammingHandout 2Input and Output ActionsI/O in HaskellQ: Of course, most programs don’t just calculate values:they have effects on the world — displaying text or graphics,reading or writing the file system and network...How does this square with Haskell ’s value-oriented,calculational style of computation?I/O in HaskellA: Haskel l provides a special kind of value, called an action,that describes an effect on the world.Pure actions, whi ch just do something and have nointeresting “result,” are values of type IO ().For example, the p utStr function takes a string and yields anaction de scribing the act of displaying this string on s tdout.putString :: String -> IO ()I/O in HaskellTo actually perform an action, we make it the value of thespecial name main.main :: IO ()main = putStr "Hello world\n"I/O in Haskell~/current/advprog/common/lectures> hugs hello.hs__ __ __ __ ____ ___ _________________________________________|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard||___|| ||__|| ||__|| __|| Copyright (c) 1994-2003||---|| ___|| World Wide Web: http://haskell.org/hugs|| || Report bugs to: [email protected]|| || Version: November 2003 _________________________________________Haskell 98 mode: Restart with command line option -98 to enable extensionsType :? for helpMain> mainHello worldMain>The Batch InterpreterThe commandrunhugs file.hswill load the file file.hs into hugs and perform the actionbound to the top-level name ma in.~/current/advprog/common/lectures> runhugs hello.hsHello world~/current/advprog/common/lectures>ActionsActions are des criptions of effects on the world. Simplywriting an action does not, by itself, cause anything tohappen.hellos :: [IO ()]hellos = [putStr "Hello s omebody\n",putStr "Hello world\n",putStr "Hello universe\n"]main = head (tail hellos)I/O in Haskell~/current/advprog/common/lectures> runhugs hellos.hsHello world~/current/advprog/common/lectures>Combining ActionsThe infix operator >> takes two actions a and b and yields anaction that describes the effect of executing a and b insequence.hello1 :: IO ()hello1 = putStr "hello " >> putStr "world\n"“Do” NotationTo avoid writing >> all the time, Haskell provides specialsyntax for sequencing actions:hello2 = do putStr "hello "putStr "world\n"In general, if act1, act 2, ..., actn are actions, thendo act1act2...actnis an action that represents performing them in sequence.Note the use of the “layout convention” here: the first actionbegins right after the do and the others are laid outvertically beneath it.Input ActionsSome actions have an effect on the world and yield a result.For example,getLine :: IO Stringis an action that, when executed, consumes the next l inefrom the standard input and returns it.“Do” Notation for Input ActionsThe do syntax provides a way to bind the result of an actionto a variable so that it can be referred to later.main =do putStr "Please type a line...\n"s <- getLineputStr "You typed ’"putStr sputStr "’\n"“Do” Notation for Input Actions~/current/advprog/common/lectures> runhugs lec2a.lhsPlease type a line...hello thereYou typed ’hello there’GraphicsGraphics in HaskellThe module S OEGraphics provides a number of actions fordrawing things on the screen.openWindow :: Title -> Size -> IO Windowtype Title = Stringtype Size = (Int,Int)A Complete Graphics Programimport SOEGraphicsg = do w <- openWindow"My First Graphics Program" (300,300)drawInWindow w(text (100,200) "Hello Graphics World")drawInWindow w(withColor Red (ellipse (0,0) (100,150)))k <- getKey wcloseWindow wmain = runGraphics gSierpinski’s TriangleSierpinski’s TrianglefillTri :: Window -> Int -> Int -> Int -> IO ()fillTri w x y size= drawInWindo w w (withColor B lue(polygon [(x,y),(x+size,y),(x,y-size),(x,y)]))Sierpinski’s TrianglesierpinskiTri :: Window -> Int -> Int -> Int -> IO ()sierpinskiTri w x y size= if size <= 8then fillTri w x y sizeelse let size2 = size ‘ div‘ 2in do sierpinskiTri w x y size2sierpinskiTri w x (y-size2) size2sierpinskiTri w (x+size2) y size2Sierpinski’s Triangleg = do w <- openWindow"Sierpinski’s Triangle" (4 00,400)sierpinskiTri w 50 300 256k <- getKey wcloseWindow wmain = runGraphics


View Full Document

Penn CIS 399 - Input and Output Actions

Download Input and Output Actions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Input and Output Actions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Input and Output Actions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?