Script
اسلاید 1: Operators, Functions and Modules1I/O
اسلاید 2: Operators, Functions and Modules2I/O functions that have this type, return a value of type a as their result.getLine :: IO Stringreads a line from the standard input stream(returns a string from input)getChar :: IO Charreads 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 functionsread (“12”) = 12show (True) = “True”show (12) = “12”putStr (5+3= ++ show (8))putStrLn (5+3= ++ show (5+3))Type IO a
اسلاید 3: Operators, Functions and Modules3An Example Script
اسلاید 4: Operators, Functions and Modules4maxSales :: Int -> IntmaxSales n | n == 0 = sales 0 | otherwise = maxi (sales n) (maxSales (n-1))You have already seen these functions.meanSales :: Int -> FloatmeanSales n = fromInt (totalSales n)/ fromInt (n+1)maxi :: Int -> Int -> Int maxi m n | n >= m = n | otherwise = msales :: Int -> Intsales 0 = 15; sales 1 = 5; sales 2 = 7; sales 3 = 18;sales 4 = 7; sales 5 = 0; sales 6 = 5totalSales :: Int -> InttotalSales n | n == 0 = sales 0 | otherwise = totalSales (n-1) + sales n
اسلاید 5: Operators, Functions and Modules5Week Sales 0 15 1 5 2 7 3 18 Total = 45 Mean = 11.25Main> printTable 6 Week Sales 0 15 1 5 2 7 3 18 4 7 5 0 6 5 Total = 57 Mean = 8.14286Main> printTable 3
اسلاید 6: Operators, Functions and Modules6printTable :: 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 We will now define a function to print the sales table.
اسلاید 7: Operators, Functions and Modules7printWeek :: 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))And to print each week’s data.
اسلاید 8: Operators, Functions and Modules8Tuples
اسلاید 9: Operators, Functions and Modules9Type Person = StringType First = StringType Last = String Type Grade = Int Type Date = StringTuplesA tuple is a collection of data items put together into a single entity. These data items can be of different types. A tupple: type Person = (String, Int) (“George”, 25) (“John”, 87) (“Andrew”, 55)Or: type Grade = (String, String, Int) (“Final”, “John Moore”, 92) (“Final”, “Joe Fan”, 98) (“Midterm”, “John Moore”, 88) (“Midterm”, “Joe Fan”, 92)Types Again
اسلاید 10: Operators, Functions and Modules10Example:type Author = Stringtype Title = Stringtype Book = (Title, Author)type StudId = Inttype Year = Inttype FirstN = Stringtype LastN = Stringtype Student = (StudId , FirstN , LastN , Year)A tuple type may be declared as follows:type tuple_type_name = (t1, t2,…, tn)
اسلاید 11: Operators, Functions and Modules11type Price = Floattype ItemNo = Inttype ItemName = Stringtype Grocery = (ItemNo , ItemName, Price )(195, “German Chocholate Cake”, 10.99)(199, “Muffins”, 0.55)(121, “Fish Sticks”, 5.95)itemNumber :: Grocery -> IntitemNumber (iNum, _, _) = iNumitemName :: Grocery -> StringitemName (_, iName, _) = iNameitemPrice :: Grocery -> FloatitemPrice (_, _, iPrice) = iPriceitemPrice :: Grocery -> (ItemNo, Price )itemPrice (i, _, p) = (i, p)
اسلاید 12: Operators, Functions and Modules12Local Definitions
اسلاید 13: Operators, Functions and Modules13Local definitions are definitions within a function that make the function more readable.func :: Float -> Float -> Float -> Floatfunc 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)/4Main> func 8 1 3-15.3125
اسلاید 14: Operators, Functions and Modules14fact :: Int -> Intfact n |n==0=1 |n>0=fact (n-1)*nfact n |n==0=1 |n>0=fact (n-1)*n |otherwise=0fact 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 numbersMain> fact -1ERROR - Unresolved overloading*** Type : (Num a, Ord a, Num (a -> a)) => a -> a*** Expression : fact - 1Error
نقد و بررسی ها
هیچ نظری برای این پاورپوینت نوشته نشده است.