Introduction to Programming in Haskell

Download Report

Transcript Introduction to Programming in Haskell

Introduction to Programming in
Haskell
By:
Mohammad Semnani
Navid Bazzaz zadeh
1
Topics
•
•
•
•
•
Expressions and values
Definitions and variables
Functions and operators
Types and type checking
Properties and testing
2
Why Haskell?
•Haskell is a very high-level language (many details taken
care of automatically).
•Haskell is expressive and concise (can achieve a lot with a
little effort).
•Haskell is not a high-performance language (prioritise
programmer-time over computer-time).
3
A Haskell Demo
• Start the hugs Haskell interpreter:
> hugs
__
__ __ __ ____
___
||
|| || || || || ||__
||___|| ||__|| ||__|| __||
||---||
___||
||
||
||
|| Version: Nov 2003
Type :? for help
Prelude>
_______________________________________________
Hugs 98: Based on the Haskell 98 standard
Copyright (c) 1994-2003
World Wide Web: http://haskell.org/hugs
Report bugs to: [email protected]
_______________________________________________
The prompt. Hugs is ready for input.
4
Hugs as a Calculator
Prelude> 2+2
4
Prelude> 2-2
0
Prelude> 2*3
6
Prelude> 2/3
0.666666666666667
Type in expressions, and
hugs calculates and prints
their value.
5
Binding and Brackets
Prelude> 2+2*3
8
Prelude> (2+2)*3
12
Prelude> (1+2)/(3+4)
0.428571428571429
Multiplication and division
”bind more tightly” than
addition and subtraction – so
this means 2+6, not 4*3.
We use brackets to change
the binding (just as in
mathematics), so this is 4*3.
This is how we write
1+2
3+4
6
Example: Currency Conversion
• Suppose we want to buy a game costing €53
from a foreign web site.
Prelude> 53*9.16642
485.82026
Exchange rate: €1 =
9.16642 SEK
Price in SEK
Boring to type 9.16642 every
time we convert a price!
7
Naming a Value
• We give a name to a value by making a
definition.
• Definitions are put in a file, using a text
editor such as emacs.
> emacs Examples.hs
The UNIX prompt,
not the hugs one!
Haskell files
end in .hs
Do this in a separate
window, so you can
edit and run hugs
simultaneously.
8
Creating the Definition
Give the name
euroRate to the
value 9.16642
variable
9
Using the Definition
The prompt
changes – we
have now
loaded a
program.
Load the file
Examples.hs into
hugs – make the
definition
available.
Prelude> :l Examples
Main> euroRate
9.16642
Main> 53*euroRate
485.82026
Main>
We are free to make use
of the definition.
10
Functional Programming is based on
Functions
• A function is a way of computing a result
from its arguments
• A functional program computes its output as
a function of its input
– E.g. Price in SEK from price in euros
11
A Function to convert Euros to SEK
A definition –
placed in the
file
Examples.hs
A comment – to help us understand
the program
-- convert euros to SEK
sek x = x*euroRate
Function name –
the name we are
defining.
Name for the
argument
Expression to
compute the
result
12
Using the Function
Main> :r
Main> sek 53
485.82026
Reload the file to make the new
definition available.
Call the function
Notation: no brackets!
C.f. sin 0.5, log 2, not f(x).
sek x = x*euroRate
x = 53
While we evaluate
the right hand side
53*euroRate
13
Converting Back Again
-- convert SEK to euros
euro x = x/euroRate
Main>
Main>
53.0
Main>
49.0
Main>
217.0
:r
euro 485.82026
euro (sek 49)
sek (euro 217)
Testing the
program: trying it
out to see if does
the right thing.
14
Automating Testing
• Why compare the result myself, when I
have a computer?
The operator == tests whether
two values are equal
Main> 2 == 2
True
Yes they are
Main> 2 == 3
No they aren’t
False
Main> euro (sek 49) == 49
Note: two equal signs
True
are an operator.
Let the computer check
the result.
One equal sign makes
15
a definition.
Defining the Property
• Define a function to perform the test for us
prop_EuroSek x = euro (sek x) == x
Main> prop_EuroSek 53
True
Main> prop_EuroSek 49
True
Performs the same
tests as before – but
now we need only
remember the
function name!
16
Writing Properties in Files
• Functions with names beginning ”prop_”
are properties – they should always return
True
• Writing properties in files
– Tells us how functions should behave
– Tells us what has been tested
– Lets us repeat tests after changing a definition
• Properties help us get programs right!
17
Automatic Testing
• Testing account for more than half the cost
of a software project
• We will use a tool for automatic testing
import QuickCheck
Add first in the file of
definitions – makes
QuickCheck available.
Capital letters must
appear exactly as they do
here.
18
Running Tests
Main> quickCheck prop_EuroSek
Falsifiable, after 10 tests:
3.75
It’s not true!
The value for which
the test fails.
Runs 100
randomly
chosen tests
19
Name the Price
• Let’s define a name for the price of the
game we want
price = 53
Main> sek price
ERROR - Type error
*** Expression
*** Term
*** Type
*** Does not match
in application
: sek price
: price
: Integer
: Double
20
Every Value has a Type
• The :i command prints information about a
name
Main> :i price
price :: Integer
Main> :i euroRate
euroRate :: Double
Integer (whole number) is
the type of price
Double is the type of real
numbers
Funny name, but refers to
double the precision that
computers originally used21
Type Checking
• Infers (works out) the type of every
expression
• Checks that all types match – before
running the program
22
Why did it work before?
Certainly works to say 53
What is the type of 53?
Main> sek 53
485.82026
Main> 53 :: Integer
53 can be used with several
53
types – it is overloaded
Main> 53 :: Double
53.0
Main> price :: Integer
Giving it a name fixes
53
the type
Main> price :: Double
ERROR - Type error in type annotation
*** Term
: price
*** Type
: Integer
23
*** Does not match : Double
Fixing the Problem
• Definitions can be given a type signature
which specifies their type
price :: Double
price = 53
Main> :i price
price :: Double
Main> sek price
485.82026
24
Always Specify Type Signatures!
• They help the reader (and you) understand
the program
• The type checker can find your errors more
easily, by checking that definitions have the
types you say
• Type error messages will be easier to
understand
• Sometimes they are necessary (as for price)
25
What is the type of 53?
Main> :i 53
Unknown reference `53'
Main> :t sek 53
sek 53 :: Double
Main> :t 53
53 :: Num a => a
If a is a numeric type
:i only works for
names
But :t gives the type (only)
of any expression
Then 53 can have type a
26
Rechecking All Properties
• The program check runs quickCheck on
every property in a file
> check Examples.hs
…
Main> prop_EuroSek: OK, passed 100 tests.
Main> prop_SinCos: OK, passed 100 tests.
Main> prop_Sqrt: Falsifiable, after 0 tests:
-1.33333333333333
27