صفحه 1:
An Example Script
Operators, Functions and
Modules
صفحه 2:
1 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; sales6= 5
meanSales :: Int -> Float
meanSales n =
fromInt (totalSales n)/ fromInt (n+1)
maxi :: Int -> Int -> Int
maxi mn
|n>=m =n
Jotherwise =m
totalSales :: Int -> Int
totalSales n
|n==0 = sales 0
| otherwise = totalSales (n-1) +
sales n
maxSales :: Int -> Int
maxSales n
] دده = sales 0
| otherwise = maxi (sales n) (maxSales (n-1))
Operators, Functions and
Modules
صفحه 3:
fain> 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 = Operators, Functions and
Mean = 8.14286
صفحه 4:
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 = اه موادم abet printWeekn 4
صفحه 5:
nd to print each week’s data.
printWeek :: Int -> String
printWeek n = " "+4 (show n) ++
" "++ (show (sales n)) ++ "ما"
Functions for printing the total and mean at the
bottom.
printTotal :: Int -> String
printTotal n =" Total ="++" ۲ +4 20117
(totalSales n))
++ "\n"
printMean :: Int -> String
printMean n = " "++ "Mean =" ++
Operators, Functions and
"— دده (show MeeteinSales n))
صفحه 6:
Tuples
Operators, Functions and
Modules
صفحه 7:
ast = String
rade = Int
ate = String
5
le is a collection of data items put together into a single en’
» data items can be of different types.
le: type Person = (String, Int)
(“George”, 25)
(“John”, 87)
(“Andrew”, 55)
type Grade = (String, String, Int)
(“Final”, “John Moore”, 92)
(‘Final”, “Joe Fan”, 98)
(“Midterm”, “John Moore”, 88)
(“Midterm”, “Joe Fan”, 92)
Operators, Functions and 7
Modules
صفحه 8:
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 = (StudlId, FirstN , LastN ,
Year)
Operators, Functions and
Modules
صفحه 9:
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)
Operators, Functions and
Modules
صفحه 10:
10
Local Definitions
Operators, Functions and
Modules
صفحه 11:
Local definitions are definitions within a function that
make the
Aine ORS Bias Float -> Float
func ab c = (sqrt (at+c)/4 + b + 3) * (sqrt (at+c)/4-b
- 3)
Defining the same function using a local definition
(i.e. using a where clause) makes the definition more
readable.
funcabc =(r+b+3)*(r-b- 3)
where r= sqrt (at+c)/4
Main> func 8 1 3 operators, Functions and 11
Modules
صفحه 12:
Error
fact :: Int -> Int
fact n
|n== =1
|n>0 =fact (n-1)*n
n== =1
|n>0 =fact (n-1)*n
otherwise
fact n
jn==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
ee Type : (Num a, Ord a, a-> a)) =>a->a
Nw
0 هگ 1 8.52 and’ 12
“ek Expression : fact - 1°? Modules