Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190...

52
Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn 2008 – Week 2 Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 1 / 44

Transcript of Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190...

Page 1: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Basic definitions – Types and FunctionsCS190 Functional Programming Techniques

Dr Hans Georg Schaathun

University of Surrey

Autumn 2008 – Week 2

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 1 / 44

Page 2: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Outline

1 Types

2 Functions

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 2 / 44

Page 3: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

This session

After this session, you shouldunderstand the role of functions in computer programmingbe able to define and use new data typesbe able to write syntactically correct Haskellbe able avoid the most common mistakes and understand the mostcommon error messages.

This lecture includes an awful lot of detailyou have to go to the lab and try it in practice

Reading: Thompson Chapter 3

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 3 / 44

Page 4: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

This session

After this session, you shouldunderstand the role of functions in computer programmingbe able to define and use new data typesbe able to write syntactically correct Haskellbe able avoid the most common mistakes and understand the mostcommon error messages.

This lecture includes an awful lot of detailyou have to go to the lab and try it in practice

Reading: Thompson Chapter 3

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 3 / 44

Page 5: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Outline

1 TypesBasicsStrings and Lists

2 Functions

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 4 / 44

Page 6: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Data

Computing is all about processing dataIntegers: credit card numbers, yearsStrings: family name, commentBoolean: true or falseFloating point numbers: weight

Representation of information is a key skill

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 5 / 44

Page 7: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Type checking

Haskell is a strongly typed languageEvery piece of data has a type

Every function accepts certain types

Incompatible types are rejected

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 6 / 44

Page 8: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Basic types

Int fixed size integers ({−231, . . . ,−2,−1, 0, 1, 2, . . . , 231 − 1}in Haskell)

Integer arbitrary size integers ({−∞, . . . ,−2,−1, 0, 1, 2, . . . ,∞})Float numbers with a decimal pointChar a single characterBool either True or False

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 7 / 44

Page 9: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Boolean

&& — and|| — ornot — not

(&&) :: Bool -> Bool -> BoolTrue && b = bFalse && _ = False(||) :: Bool -> Bool -> BoolFalse || b = bTrue || _ = Truenot :: Bool -> Boolnot False = Truenot True = False

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 8 / 44

Page 10: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

The integers (Int)

5 + 3 = 8 (addition)5 * 3 = 15 (multiplication)5 ˆ 3 = 125 (exponentiation)5 - 3 = 2 (subtraction)div 5 3 = 1 (integer division)mod 5 2 = 2 (remainder)negate 5 = -5 (change sign)abs 5 = 5 (absolute value)

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 9 / 44

Page 11: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Relational operators

== equal/= not equal> greater than< less than>= greater than or equal<= less than or equal

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 10 / 44

Page 12: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Basics

Some examples

Test if a number is even: x mod 2 == 0Rounding to an integer (Float -> Int)

floor bxc: floor xceiling dxe: ceiling xrounding (nearest integer): round x

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 11 / 44

Page 13: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Strings and Lists

Outline

1 TypesBasicsStrings and Lists

2 Functions

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 12 / 44

Page 14: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Strings and Lists

Lists

For every data type there is a list type[Int] (list of integers)[Char] aka. String (lists of characters)

name :: Stringname = "George"primes :: [Int]primes = [2, 3, 5, 7, 11, 13, 17, 19, 23]

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 13 / 44

Page 15: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Strings and Lists

Strings and Names

Notice the difference between myName and "myName"Many encountered this problem in the first lab

Main> greet GeorgeERROR - Undefined data constructor "George"

Main> greet "George""Hello George"

George is an undefined name (means nothing)"George" is a data object of type String

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 14 / 44

Page 16: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Strings and Lists

Strings and Names

Notice the difference between myName and "myName"Many encountered this problem in the first lab

Main> greet GeorgeERROR - Undefined data constructor "George"

Main> greet "George""Hello George"

George is an undefined name (means nothing)"George" is a data object of type String

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 14 / 44

Page 17: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Strings and Lists

Strings and Names

Notice the difference between myName and "myName"Many encountered this problem in the first lab

Main> greet GeorgeERROR - Undefined data constructor "George"

Main> greet "George""Hello George"

George is an undefined name (means nothing)"George" is a data object of type String

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 14 / 44

Page 18: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Types Strings and Lists

Strings and Names

Notice the difference between myName and "myName"Many encountered this problem in the first lab

Main> greet GeorgeERROR - Undefined data constructor "George"

Main> greet "George""Hello George"

George is an undefined name (means nothing)"George" is a data object of type String

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 14 / 44

Page 19: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

Outline

1 Types

2 FunctionsThe Concept of a FunctionDefining functionsHigher-Order FunctionsOverloading

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 15 / 44

Page 20: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

What is a function?

x1 function y1

View the function as a machineA value x1 comes inA value y1 is returned

Using a function, it is a black boxYou don’t know what happens insideYou see only the output y1.

Mathematical conceptNo difference between a function in Haskell and in Maths

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 16 / 44

Page 21: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

What is a function?

x1 function y1

View the function as a machineA value x1 comes inA value y1 is returned

Using a function, it is a black boxYou don’t know what happens insideYou see only the output y1.

Mathematical conceptNo difference between a function in Haskell and in Maths

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 16 / 44

Page 22: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

What is a function?

x1 function y1

View the function as a machineA value x1 comes inA value y1 is returned

Using a function, it is a black boxYou don’t know what happens insideYou see only the output y1.

Mathematical conceptNo difference between a function in Haskell and in Maths

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 16 / 44

Page 23: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

Several input parameters

x1

x0

x2

function y1

A function can take more than one inputInput parameters (or arguments) x0, x1, x2

Return value (or output parameter) yHaskell does not allow multiple output parameters

though the return value can be a list or tuple (see later)

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 17 / 44

Page 24: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

Modular programming

A key element in software design is modularisationsplit the problem into smaller piecessolve each piece separatelysome partial solutions may apply to other problems too

reusability

Functions is an important form of partial solutionThis applies for all problem solving for that matter

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 18 / 44

Page 25: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions The Concept of a Function

Good function design

Each function has a clear purposeWell-defined inputs and outputsWell-written documentation

easy for others to understand and use the functionDocumentation includes both

1 in-line comments2 separate user manual

Good overview of the functionseasy to find the right function

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 19 / 44

Page 26: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Defining functions

Outline

1 Types

2 FunctionsThe Concept of a FunctionDefining functionsHigher-Order FunctionsOverloading

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 20 / 44

Page 27: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Defining functions

GuardsConditional definitions

Function definitions may be conditional

f (x) =

x3 if x > 0,

x2 if x < 0,

1 otherwise

(1)

f :: Int -> Intf x| x > 0 = x^3| x < 0 = x^2| otherwise = 1

The first applicable condition appliesGood style to use non-overlapping conditions

I.e. only one applicable condition for every case

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 21 / 44

Page 28: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Defining functions

Functions versus Operators

Haskell allows bothprefix notation: div 5 3;and infix notation: 5.0 / 3;

We often distinguish betweenfunctions – using prefix notationoperators – using infix notation

However, the difference is stylistic onlyparentheses makes a function from an operator

(+) 5 3

backticks makes an operator from a function5 ‘div‘ 3

Try it!

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 22 / 44

Page 29: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Defining functions

Defining new operators

New operators are declared function style(+.+) :: Int -> Int -> Int

They can be defined either as functions or operators(+.+) a b = 2*a + 10*ba +.+ b = 2*a + 10*b

Operators use special characters> < = - . + : / et c.cannot start with a colon (:)

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 23 / 44

Page 30: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Higher-Order Functions

Outline

1 Types

2 FunctionsThe Concept of a FunctionDefining functionsHigher-Order FunctionsOverloading

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 24 / 44

Page 31: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Higher-Order Functions

Higher-Order Functions

Functions as argument to functionse.g. foobar :: ( a -> b ) -> [a] -> [b]

foobar is a higher-order functionNote: no conceptual difference between functions and dataThe typical application is

applying a function on elements in a list

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 25 / 44

Page 32: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Higher-Order Functions

map

map :: ( a -> b ) -> [a] -> [b]map f xs

apply f on each element of xs

map f [x1, x2, . . . xn] = [fx1, fx2, . . . fxn]

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 26 / 44

Page 33: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Overloading

Outline

1 Types

2 FunctionsThe Concept of a FunctionDefining functionsHigher-Order FunctionsOverloading

3 Principles

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 27 / 44

Page 34: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Overloading

Overloading

Many operators refer to multiple operationsdifferent meanings are overloaded on the same symbol

For instance:(==) :: Bool -> Bool -> Bool(==) :: Int -> Int -> Bool

Hugs> False == FalseTrueHugs> 3 == 4FalseHugs> 3 == FalseERROR - Cannot infer instance

*** Instance : Num Bool

*** Expression : 3 == False

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 28 / 44

Page 35: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Functions Overloading

Why overloading?

1 Simplifies notation2 Supports polymorphism – to be introduced later

Not all languages support overloadingStrong typing is a prerequisiteThe computer has to know the type to choose the right meaning

Also literals can be overloaded2 :: Int2 :: Float

You can make your own overloaded functions... we will return to this in a later week.

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 29 / 44

Page 36: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Principles A pure functional language

Outline

1 Types

2 Functions

3 PrinciplesA pure functional language

4 Syntax and Layout

5 Errors

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 30 / 44

Page 37: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Principles A pure functional language

Pure functional language

Haskell is a pure functional languageIt adhers to a strict set of principlesFascilitates formal reasoning

The code is easier to understand

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 31 / 44

Page 38: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Principles A pure functional language

Side-effects

Functions have no side-effectall communication is via the outputcannot modify behaviour of other functionscannot do Input/Output (IO) directly

Avoiding side-effects is good practiceeasier to reuse codeother applications often need

same return valuedifferent side-effects (output)

e.g. computational back-end without IO can be combined withdifferent user interfaces

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 32 / 44

Page 39: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Principles A pure functional language

Referential transparency

All calls to the same functionsame input ⇒ same output

Two majore benefitsthe compiler can optimise (avoid recomputation)the developer can make mathematical proofs

Referential transparency is a consequence of avoidingside-effects

side-effects modify future behaviour of the program

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 33 / 44

Page 40: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Principles A pure functional language

Imperative programmingincluding Java

Global state (memory)methods (functions) can change the statemethod behaviour can depend on state

Different calls to f can give different outputtypical example: getchar (read keyboard input)one call removes a character from input buffer

changes the value of the next call

Functional principles can be used in imperative languagesavoid side-effects when they are not necessaryseparate computations and IO

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 34 / 44

Page 41: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Principles A pure functional language

Imperative versus Functional

Imperative programming:list of actions to be performed by the computer

Functional programming:list of mathematical definitions and declarations

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 35 / 44

Page 42: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

Associativity

4 + 5 + 12 + 1how is this evaluated?which + is evaluated first?is 4+5 and 12+1 arguments to the second plus?or is 4 and 5+12+1 arguments to the first plus?

Parentheses resolve the ambiguity(4+5) + (12+1)4 + (5+12+1) (second term still ambiguous)

Addition is associative, so it does not matter

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 36 / 44

Page 43: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

Non-associativity

- is not associative(5− 4)− 2 6= 5− (4− 2)

What does 5-4-2 mean?- is left associative... applied left to righti.e. 5− 4− 2 = (5− 4)− 2

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 37 / 44

Page 44: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

Binding powers

Consider 5 + 4*2(5+4)*2 or 5 + (4*2)?

The mathematical convention ismultiplication is applied before addition/subtraction

This is called binding power* has 7 ; +/- have 6hence * is applied first (5 + (4*2))

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 38 / 44

Page 45: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

Binding powers

Consider 5 + 4*2(5+4)*2 or 5 + (4*2)?

The mathematical convention ismultiplication is applied before addition/subtraction

This is called binding power* has 7 ; +/- have 6hence * is applied first (5 + (4*2))

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 38 / 44

Page 46: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

Binding powers

Consider 5 + 4*2(5+4)*2 or 5 + (4*2)?

The mathematical convention ismultiplication is applied before addition/subtraction

This is called binding power* has 7 ; +/- have 6hence * is applied first (5 + (4*2))

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 38 / 44

Page 47: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

Warning: function application

The tightest binding is funcion applicationf x+y means (f x) + y

composite arguments have to be parenthesisedf(x+y)

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 39 / 44

Page 48: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Syntax and Layout

General advice

1 Mathematical intuition is usually correct2 [Thompson] has a list of binding powers (Appendix C)3 If in doubt, use parentheses

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 40 / 44

Page 49: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Errors

Common Error MessagesSyntax errors

Hugs> 2 + (3+4ERROR - Syntax error in expression (unexpected end of input)Hugs> 2 + (3+4))ERROR - Syntax error in input (unexpected ‘)’)Hugs>

The input does not make any senseHaskell tells you where it discovered the error

that may or may not be the location of the error

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 41 / 44

Page 50: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Errors

Common Error MessagesType errors

Hugs> not (&&)ERROR - Type error in application

*** Expression : not (&&)

*** Term : (&&)

*** Type : Bool -> Bool -> Bool

*** Does not match : Bool

Hugs expects an argument of type Bool

... but finds one of type Bool -> Bool -> Bool

This illustrates the strong typing of Haskell

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 42 / 44

Page 51: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Errors

Common Error MessagesType errors and overloading

Error messages can be hard to readFor instance, when overloading is involved:

Hugs> abs notERROR - Cannot infer instance

*** Instance : Num (Bool -> Bool)

*** Expression : abs not

abs is defined for many typesHugs does not know exactly what it is looking for... but it is evidently not what it finds

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 43 / 44

Page 52: Basic definitions – Types and Functions · Basic definitions – Types and Functions CS190 Functional Programming Techniques Dr Hans Georg Schaathun University of Surrey Autumn

Errors

Common Error MessagesMissing function arguments

Hugs> minERROR - Cannot find "show" function for:

*** Expression : min

*** Of type : Integer -> Integer -> Integer

The problem here is trivialmin takes two arguments

However, the error message concerns printabilityFunctions are objects, like any other data

The expression min is a valid expression... the type is Int -> Int -> Int

The problem is that hugs tries to display the objectThis requires a function show which does not exist

Dr Hans Georg Schaathun Basic definitions – Types and Functions Autumn 2008 – Week 2 44 / 44