1 / 9

CS 152, Programming Paradigms Fall 2012, SJSU

CS 152, Programming Paradigms Fall 2012, SJSU. Jeff Smith. Input and output. I/O functions are called actions. Actions have a return type labeled with “IO”. print :: Show a => a -> IO () putChar :: Char -> IO () getChar :: IO Char putStr :: String -> IO ()

liang
Download Presentation

CS 152, Programming Paradigms Fall 2012, SJSU

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 152, Programming ParadigmsFall 2012, SJSU Jeff Smith

  2. Input and output • I/O functions are called actions. • Actions have a return type labeled with “IO”. • print :: Show a => a -> IO () • putChar :: Char -> IO () • getChar :: IO Char • putStr :: String -> IO () • putStrLn :: String -> IO ()

  3. IO types and encapsulation • IO types are similar to Java wrapper classes • They encapsulate a value of a specified type • e.g., IO Char wraps a character • e.g., IO () wraps nothing • We’ll see later how values are unwrapped • and wrapped

  4. Using actions • Actions can be evaluated by the interpreter • e.g. print ("abc" ++ "cde“) • Actions can be the body of a mainfunction • e.g., main = putStrLn "howdy!!"

  5. I/O and main functions • Main bodies can have multiple actions. • They need to be introduced by do e.g. main = do print "enter a letter: " c <- getChar putStrLn "" print (replicate 5 c) • Here the <- operator unwraps the character

  6. Exceptions, I/O, functional programming • Note that I/O is not congenial to the functional paradigm • e.g., getChar does not return the same value every time it is called • Haskell has a full-fledged exception mechanism available (and appropriate) for I/O.

  7. The main function • Should have the only exceptional behavior of a module • Is what is run from the command line (perhaps with parameters) • Can be run from the Actions menu, by pressing F5, or by entering :m

  8. Functions for printing • print is equivalent to putStrLn . show • putStrandputStrLndo not coerce their arguments to be strings • unlike toString in Java

  9. Wrapping by I/O types • To wrap a value for an I/O type, use return • Do not confuse this with return in Java • It does not stop execution in any sense. • Examples of its use can be seen in the definition of the Prelude module.

More Related