Lecture -*- Outline -*- * programs in Haskell Based on material from Haskell.org See "Introduction to IO" on the Haskell Wiki (https://www.haskell.org/haskellwiki/Introduction_to_IO) ** modules ------------------------------------------ PROGRAMS ARE MODULES WITH Main One module, named Main, which exports main module Main where -- main :: IO () main = do ... ------------------------------------------ The module declaration can be omitted, defaults to Main ------------------------------------------ SIMPLE I/O EXAMPLE The type of a main program is an IO action > main :: IO() > main = do > putStr "Hello! What is your name? " > name <- getLine > putStrLn ("Hello, " ++ name ++ "!") ------------------------------------------ in file SimpleIO.lhs ** IO types ------------------------------------------ IO TYPES I/O must happen in an action with type: IO a meaning CAN ONLY DO I/O IN AN IO TYPE CONTEXT The following are type errors: 3 + (do {read getLine} :: Integer) 3 + (log x) + 7 where log x = do putStrLn x return x ------------------------------------------ ... I/O actions that return a value of type a ... Can't use an (IO Integer) as an Integer! ** Normal program structure ------------------------------------------ PROGRAM STRUCTURE main :: IO () main = do vals <- inputAct let res = compute vals outputAct res inputAction :: IO ValType ... outputAction :: ResType -> IO () ------------------------------------------ Note that can't use <- notation where the right hand side is not an IO action (of type IO a for some a) Can use "let" notation however to name intermediate results Explain how the IO types work ------------------------------------------ A PICTURE |-------------| | | ========>| | =========> |-------------| ------------------------------------------ ... label the arrows inputAct and outputAct label the box compute Point out which is the "main" program Haskell can also handle interactive programs, but the details are complex, we will cover later *** example temperature conversion **** file TempConvertMain.lhs, the main program ------------------------------------------ A program is a set of modules, one of which must be called Main and export the name "main". If a file does not have a module declaration, then it is implicitly named "Main", so that is what is done with this file. > import TemperatureConversion > import Control.Exception.Base > > main :: IO() > main = do > catch (loop rcp) bye The loop function does an IO action forever, until the action throws an exception > loop :: IO () -> IO () > loop act = do > act > loop act The bye function takes an IOException, prints "bye!", and returns (), > bye :: IOException -> IO () > bye _ = do > putStrLn "bye!" > return () The rcp function reads a Double, then prints its conversion > rcp :: IO () > rcp = do > hSetBuffering stdin NoBuffering -- fix buffering > hSetBuffering stdout NoBuffering > ftemp <- ask > putStrLn ("in degrees C is: " ++ show (convert ftemp)) The ask function obtains a temperature in Farenheit from the user. > ask :: IO Double > ask = do > putStr "Temp. in degrees F? " > temp <- getLine > f <- readIO temp > return f ------------------------------------------ **** TemperatureConversion module (where the computations go) This is the part we will focus on ------------------------------------------ module TemperatureConversion where convert :: Double -> Double convert f = (f - 32) * (5/9) ------------------------------------------