- Find up-to-date information relevant for the course
- Visit the course homepage. Shortcut:
bit.ly/tda452-home
- Schedule
- Lab assignments (first deadline in 1 week!)
- Exercises
- Last-minute changes
- Sign up for the google group!

- It will make you think differently about programming.
- Mainstream programming is all about
*state*and how to*transform*state. - Functional programming is all about
*values*and how to construct values using*functions*.

- Mainstream programming is all about
- It will make you a better programmer
- whatever programmer language you use in the future.

- It's a very high-level language
- It's expressive and concise
- It's good at handling complex data
- It's makes it easy to write modular and reusable code
- It's good for programmer productivity and software quality
- It's the most uncompromising functional programming language.

- The most mature and widely used implementation of Haskell.
- It's an advanced optimizing compiler.
- It also includes an interpreter (REPL) for easy testing (GHCi).
- Installing the Haskell Platform should be easy and gives you what you need.

`ghci`

GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
` `

- Source code: Intro.hs

- Write this in a Haskell file, e.g.
`Intro.hs`

. `exchangeRate`**=**9.7365`toEUR``sek`**=**`sek``/``echangeRate``toSEK``eur`**=**`eur``*``exchangeRate`- Illustrates definitions of constants and functions.
- Haskell is a
*layout sensitive language*.- It is possible to use braces and semicolons instead...

`:load Intro.hs`

[1 of 1] Compiling Main ( Intro.hs, interpreted ) Ok, modules loaded: Main.`toEur 85`

8.730036460740513`toSEK 9`

87.6285`toSEK (toEUR 85)`

85.0- You can use
`:reload`

to reload the file(s) after changes. - You can use
`:l`

and`:r`

as abbreviations for`:load`

and`:reload`

.

`prop_exchange``s`**=**`toSEK`(`toEUR``s`)`==``s`- By convention, functions whose names start with
`prop_`

are properties we expect to hold. - Writing down properties
- documents
*how*functions should behave. - tells us
*what*to test / what has been tested. - makes it easy to
*repeat*tests after changes.

- documents

- Import the testing library:
**import****Test.QuickCheck**- Imports go before any other definitions in the file

- Reload and run a test:
`:r`

[1 of 1] Compiling Main ( Intro.hs, interpreted ) Ok, modules loaded: Main.`quickCheck prop_exchange`

*** Failed! Falsifiable (after 5 tests and 1078 shrinks): 5.0e-324

- Floating point calculations are not exact, so you can not expect exact equality test to succeed.
- We need an approximate equality test!

`x``~==``y`**=**`x``-``y``<`1`e``-`10- Defining a new operator is just like defining a new function.
- Operators have symbolic names, normal functions have alphanumeric names.

- Using it:
`prop_exchange``s`**=**`toSEK`(`toEUR``s`)`~==``s`

`:r`

[1 of 1] Compiling Main ( Intro.hs, interpreted ) Ok, modules loaded: Main.`quickCheck prop_exchange`

+++ OK, passed 100 tests.- But is everything OK now?

`4 ~== 3`

False`3 ~== 4`

True- Fixing the new equality test:
`x``~==``y`**=**`abs`(`x``-``y`)`<`1`e``-`10- After this, everything is OK!

- If x is positive, the result is x
- If x is negative, the result is -x
-- | Compute the absolute value of x

`absolute``x`**|**`x``>=`0**=**0`absolute``x`**|**`x``<`0**=**`-``x`

- Repeated left hand sides can be abbreviated:
`absolute``x`**|**`x``>=`0**=**0`absolute``x`**|**`x``<`0**=**`-``x``absolute``x`**|**`x``>=`0**=**0**|**`x``<`0**=**`-``x`

- Haskell also has
**if-then-else**:`absolute``x`**=****if**`x``<`0**then**`-``x`**else**`x`

- First example of a
*recursive*function- Compute
`x`(without using built-in^{n})`x``^``n` `power``x`0**=**1`power``x``n`**|**`n``>`0**=**`x``*``power``x`(`n``-`1)

- Compute
- How the result is computed:
`power`3 2

=3

`*``power`3 (2`-`1)

=3

`*``power`3 1

=3

`*`(3`*``power`3 (1`-`1))

=3

`*`(3`*``power`3 0)

=3

`*`(3`*`1)

=3

`*`3

=`9`

- Reduce a problem (e.g. ) to a
`power``x``n`*smaller*problem of the same kind, - so that we eventually reach a "smallest"
*base case*. - Solve the base case separately.
- Build up complete solutions from smaller solutions.
- You should have seen this before...

`intersect`0**=**0`intersect``n`**|**`n``>`0**=**`intersect`(`n``-`1)`+``n``-`1- Always pick the base case as simple as possible!

- Haskell is a
*strongly typed*language- Every value/expression/function has a type.
- If there is a type mismatch somewhere in a program, it will be rejected by the compiler.

- Haskell supports
*automatic type inference*- You can omit the types most of the time, Haskell will figure it out anyway.
- It is considered good practice to always write down the types of functions anyway!

- In GHCi
- Use
`:type`

to ask for the type of an expression. - Use
`:browse`

to see the type of everything in the current module.

- Use

- You can ask for the type of an function (or a bigger expression):
`:t toEUR`

toEUR :: Double -> Double- You can see the types of everything in the current module:
`:browse`

exchangeRate :: Double toEUR :: Double -> Double toSEK :: Double -> Double prop_exchange :: Double -> Bool (~==) :: (Ord a, Fractional a) => a -> a -> Bool power :: (Ord t1, Num t1, Num t) => t -> t1 -> t

- The type is more general than we intended, it allows any numeric type
e.g. .
**Double** - What is ?
`power`2 1.5 - We'd better add a
*type signature*to restrict the type: `power`**::****Integer****->****Integer****->****Integer**`power``n`0**=**1`power``n``k`**|**`k``>`0**=**`n``*``power``n`(`k``-`1)

- It makes the code easier to understand.
- Both for you and other people who read your code.

- It can make type errors easier to understand.
- Sometimes the automatically inferred type is not the one you want.