140 likes | 277 Views
I/O. Type IO a. I/O functions that have this type, return a value of type a as their result. getLine :: IO String reads a line from the standard input stream (returns a string from input) getChar :: IO Char reads a character from the standard input stream
E N D
I/O Lecture 17 I/O, Tuples and Local Definitions
Type IO a I/O functions that have this type, return a value of type a as their result. getLine :: IO String reads a line from the standard input stream (returns a string from input) getChar :: IO Char reads a character from the standard input stream (returns a character from input) Type IO ( ) I/O functions that have this type, return a value of ( ) as their result. putStr :: String -> IO ( ) putStr “Hello World” Two other built-in functions read (“12”) = 12 show (True) = “True” show (12) = “12” Lecture 17 I/O, Tuples and Local Definitions
An Example Script Lecture 17 I/O, Tuples and Local Definitions
You have already seen these functions. sales :: Int -> Int sales 0 = 15; sales 1 = 5; sales 2 = 7; sales 3 = 18;sales 4 = 7; sales 5 = 0; sales 6 = 5 meanSales :: Int -> Float meanSales n = fromInt (totalSales n)/ fromInt (n+1) maxi :: Int -> Int -> Int maxi m n | n >= m = n | otherwise = m totalSales :: Int -> Int totalSales n | n == 0 = sales 0 | otherwise = totalSales (n-1) + sales n maxSales :: Int -> Int maxSales n | n == 0 = sales 0 | otherwise = maxi (sales n) (maxSales (n-1)) Lecture 17 I/O, Tuples and Local Definitions
Main> printTable 3 Week Sales 0 15 1 5 2 7 3 18 Total = 45 Mean = 11.25 Main> printTable 6 Week Sales 0 15 1 5 2 7 3 18 4 7 5 0 6 5 Total = 57 Mean = 8.14286 Lecture 17 I/O, Tuples and Local Definitions
We will now define a function to print the sales table. printTable :: Int -> IO () printTable n = putStr (heading ++ printUpTo n ++ printTotal n ++ printMean n) Lets define heading now. heading :: String heading = " Week" ++ " Sales" ++ "\n“ A recursive function for printing the data. printUpTo :: Int -> String printUpTo 0 = printWeek 0 printUpTo n = printUpTo (n - 1) ++ printWeek n Lecture 17 I/O, Tuples and Local Definitions
And to print each week’s data. printWeek :: Int -> String printWeek n = " " ++ (show n) ++ " "++ (show (sales n)) ++ "\n" Functions for printing the total and mean at the bottom. printTotal :: Int -> String printTotal n = " Total =" ++ " " ++ (show (totalSales n)) ++ "\n" printMean :: Int -> String printMean n = " "++ "Mean =" ++ " "++ (show (meanSales n)) Lecture 17 I/O, Tuples and Local Definitions
Tuples Lecture 17 I/O, Tuples and Local Definitions
Types Again Type Person = String Type First = String Type Last = String Type Grade = Int Type Date = String Tuples A tuple is a collection of data items put together into a single entity. These data items can be of different types. A tupple: typePerson = (String, Int) (“George”, 25) (“John”, 87) (“Andrew”, 55) Or: typeGrade = (String, String, Int) (“Final”, “John Moore”, 92) (“Final”, “Joe Fan”, 98) (“Midterm”, “John Moore”, 88) (“Midterm”, “Joe Fan”, 92) Lecture 17 I/O, Tuples and Local Definitions
A tuple type may be declared as follows: type tuple_type_name = (t1, t2,…, tn) Example: type Author = String type Title = String type Book = (Title, Author) type StudId = Int type Year = Int type FirstN = String type LastN = String type Student = (StudId , FirstN , LastN , Year) Lecture 17 I/O, Tuples and Local Definitions
type Price = Float type ItemNo = Int type ItemName = String type Grocery = (ItemNo , ItemName, Price ) (195, “German Chocholate Cake”, 10.99) (199, “Muffins”, 0.55) (121, “Fish Sticks”, 5.95) itemNumber :: Grocery -> Int itemNumber (iNum, _, _) = iNum itemName :: Grocery -> String itemName (_, iName, _) = iName itemPrice :: Grocery -> Float itemPrice (_, _, iPrice) = iPrice itemPrice :: Grocery -> (ItemNo, Price ) itemPrice (i, _, p) = (i, p) Lecture 17 I/O, Tuples and Local Definitions
Local Definitions Lecture 17 I/O, Tuples and Local Definitions
Local definitions are definitions within a function that make the function more readable. func :: Float -> Float -> Float -> Float func a b c = (sqrt (a+c)/4 + b + 3) * (sqrt (a+c)/4 - b - 3) Defining the same function using a local definition (i.e. using a where clause) makes the definition more readable. func a b c = (r + b + 3) * (r - b - 3) where r = sqrt (a+c)/4 Main> func 8 1 3 -15.3125 Lecture 17 I/O, Tuples and Local Definitions
Error fact :: Int -> Int fact n |n==0 =1 |n>0 =fact (n-1)*n fact n |n==0 =1 |n>0 =fact (n-1)*n |otherwise =0 fact n |n==0 =1 |n>0 =fact (n-1)*n |otherwise =error “factorial is only defined over natural numbers” Main> fact (-1) Program error: factorial is only defined over natural numbers Main> fact -1 ERROR - Unresolved overloading *** Type : (Num a, Ord a, Num (a -> a)) => a -> a *** Expression : fact - 1 Lecture 17 I/O, Tuples and Local Definitions