IO - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

IO

Description:

I/O functions that have this type, return a value of type a as their. result. ... type LastN = String. type Student = (StudId , FirstN , LastN , Year) ... – PowerPoint PPT presentation

Number of Views:116
Avg rating:3.0/5.0
Slides: 15
Provided by: hasa
Category:
Tags: lastn

less

Transcript and Presenter's Notes

Title: IO


1
I/O
2
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 -gt IO ( ) putStr
Hello World Two other built-in functions read
(12) 12 show (True) True show (12)
12
3
An Example Script
4
You have already seen these functions.
meanSales Int -gt Float meanSales n
fromInt (totalSales n)/ fromInt (n1) maxi
Int -gt Int -gt Int maxi m n n gt
m n otherwise m sales
Int -gt Int sales 0 15 sales 1 5 sales 2
7 sales 3 18sales 4 7 sales 5 0
sales 6 5 totalSales Int -gt Int totalSales
n n 0 sales 0
otherwise totalSales (n-1) sales
n maxSales Int -gt Int maxSales n n 0
sales 0 otherwise maxi
(sales n) (maxSales (n-1))
5
Maingt printTable 3
Week Sales 0
15 1 5
2 7 3
18 Total 45
Mean 11.25 Maingt printTable 6
Week Sales 0
15 1 5
2 7
3 18 4
7 5 0
6 5
Total 57 Mean
8.14286
6
We will now define a function to print the sales
table.
printTable Int -gt 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
-gt String printUpTo 0 printWeek 0 printUpTo n
printUpTo (n - 1) printWeek n
7
And to print each weeks data.
printWeek Int -gt String printWeek n "
" (show n) "
" (show (sales n)) "\n" Functions for
printing the total and mean at the
bottom. printTotal Int -gt String printTotal n
" Total " " " (show
(totalSales n))
"\n" printMean Int -gt String printMean
n " " "Mean "
" " (show (meanSales n))
8
Tuples
9
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
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)
10
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)
11
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 -gt
Int itemNumber (iNum, _, _) iNum itemName
Grocery -gt String itemName (_, iName, _)
iName itemPrice Grocery -gt Float itemPrice
(_, _, iPrice) iPrice itemPrice Grocery -gt
(ItemNo, Price ) itemPrice (i, _, p) (i, p)
12
Local Definitions
13
Local definitions are definitions within a
function that make the function more readable.
func Float -gt Float -gt Float -gt Float func a b
c (sqrt (ac)/4 b 3) (sqrt (ac)/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
(ac)/4 Maingt func 8 1 3 -15.3125
14
Error
fact Int -gt Int fact n n0 1
ngt0 fact (n-1)n fact n n0 1
ngt0 fact (n-1)n otherwise 0 fact n
n0 1 ngt0 fact (n-1)n
otherwise error factorial is only defined over
natural numbers Maingt fact (-1) Program error
factorial is only defined over natural
numbers Maingt fact -1 ERROR - Unresolved
overloading Type (Num a, Ord a, Num
(a -gt a)) gt a -gt a Expression fact - 1
Write a Comment
User Comments (0)
About PowerShow.com