Numbers

advertisement
COMPUTER SCIENCE 123
Foundations of Computer Science
3. Basic data types—numbers
Summary: This lecture introduces the basic numeric data
types in Haskell.
You should also have: Tutorial sheet 1
Lab sheet 2
Reference: Thompson Chapter 2, Sections 3.2 and 3.6–7
© R.L. While, 2000–3
Data types
•
•
•
•
•
•
Data types are a way of dividing up the universe of values
− every value belongs to exactly one data type
Data types can be either built-in or user-defined
− built-in data types are defined by the system
− user-defined data types are defined in the program
− cf. built-in functions vs. user-defined functions
Orthogonally to this, data types can be either simple or
structured
Simple built-in data types include
− integers Int, Integer
− real numbersFloat, Double
− BooleansBool (Lecture 4)
− charactersChar (Lecture 4)
Structured built-in data types include
− stringsString (Lecture 5)
− tuplesfor example (Int, Bool) (Lecture 6)
− functionsfor example Int -> Bool (Lecture 13)
We will discuss user-defined data types later in cs123
cs123 Foundations of CS
1 of 7
3. Basic data types—numbers
Integersthe types Int and Integer
•
•
The type Int contains positive and negative integers
− in the range −231 to 231−1
Operators defined on Int include
+
*
^
`div`
`mod`
•
•
addition
subtraction
multiplication
exponentiation
integer division
remainder
The operator `div` performs integer division
− 14 `div` 4 == 3
The operator `mod` returns the remainder after integer
division
− 14 `mod` 4 == 2
− x `mod` y always lies between 0 and y-1
x == (x `div` y) * y + x `mod` y
•
•
The quotes enclosing `div` and `mod` are back-quotes
− found on the top-left of the keyboard (with ~)
Negation involves a little syntactic twist
− a negative number that is an argument to a
function must be enclosed in brackets
is illegal
is correct
square – 3
square (– 3)
•
for all x,y
The type Integer is essentially the same as Int, except
that it can represent any integer
− we will largely ignore Integer in cs123
cs123 Foundations of CS
2 of 7
3. Basic data types—numbers
Precedence and associativity
Operator
^
*
`div` , `mod`, /
+, -
•
•
Associativity
right
left
left
left
The precedence of an operator tells us how tightly it
“binds” its arguments
− it is not necessary to remember the numbers, only the
ordering
The associativity of an operator tells us which way
adjacent applications “lean”
−
right-associative:
(x^y)^z
−
/=
x^y^z
==
x^(y^z)
x-y-z
/=
x-(y-z)
left-associative:
(x-y)-z
•
Precedence
8
7
7
6
==
Brackets can be used to overcome the defaults
cs123 Foundations of CS
3 of 7
3. Basic data types—numbers
Examples
•
Consider a function sumnats that takes a number n and
sums the first n positive integers, i.e.
1 + 2 + 3 + ... + (n − 1) + n
n
=
∑i
=
i =1
n(n + 1)
2
sumnats :: Int -> Int
-- sumnats n returns the sum of the
-- first n positive integers
sumnats n = n * (n + 1) `div` 2
•
More generally, an arithmetic series is a sequence of
numbers such that there is a constant difference d
between consecutive elements
a + ( a + d ) + ( a + 2 d ) + ... + ( a + (n − 2)d ) + ( a + (n − 1)d )
n −1
= ∑ a + kd = n(2a + (n − 1)d )
2
k =0
•
Consider a function arithmetic that takes three
numbers a, n and d and sums the a,n,d arithmetic series
arithmetic :: Int -> Int -> Int -> Int
-- arithmetic a n d returns the sum
-- of the a,n,d arithmetic series
arithmetic a n d
= n * (2 * a + (n - 1) * d) `div` 2
cs123 Foundations of CS
4 of 7
3. Basic data types—numbers
Real numbersthe types Float and Double
•
•
•
The type Float contains positive and negative real
numbers
These can be written in two ways
− decimal notation:
265.65
− scientific notation:
2.6565E2
− in general:
xEy == x * 10 ^ y
Operators defined on Float include
+
*
^
/
•
•
•
addition
subtraction
multiplication
exponentiation
division
The operator / performs real division
− 14 / 4 == 3.5
Negation involves the same syntactic twist as with Int
The type Double is essentially the same as Float,
except that it represents numbers to a greater precision
− we will largely ignore Double in cs123
cs123 Foundations of CS
5 of 7
3. Basic data types—numbers
Examples
•
•
A straight line on a graph can be represented by its slope
m and its y-intercept c
Consider a function x’intercept that takes two
numbers m, c and returns the x-intercept of the line m, c
x'intercept :: Float -> Float -> Float
-- x'intercept m c returns the x-intercept
-- of the line m, c
-- pre: m /= 0
x'intercept m c = - c / m
•
A quadratic equation has the form
ax 2 + bx + c = 0
•
The function numberNDroots returns the number of real
roots of a quadratic equation, given that a /= 0
numberNDroots :: Float->Float->Float->Int
-- numberNDroots a b c returns the number
-- of real roots of a quadratic equation
-- pre: a /= 0
numberNDroots a b c
| z > 0 = 2
| z == 0 = 1
| z < 0 = 0
where z = b ^ 2 - 4 * a * c
cs123 Foundations of CS
6 of 7
3. Basic data types—numbers
The Prelude
•
•
•
The Haskell Prelude is the main built-in library of Haskell
functions
− it is pre-loaded whenever Hugs is started
It is important to become familiar with the functions in
the Prelude
− if someone else has already written a function, clearly
it is better to use theirs than to write your own!
This is best achieved through experience
cs123 Foundations of CS
7 of 7
3. Basic data types—numbers
Download