Formal Models of Computation -...

Post on 05-Apr-2021

3 views 0 download

Transcript of Formal Models of Computation -...

Formal Models of ComputationLecture I(3) – Introduction to Haskell

Kees van Deemter, Room 221 (based on materials by Wamberto Vasconcelos and Chris Mellish)

k.vdeemter@.abdn.ac.uk

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.1/16

Plan of Lecture

1. Bibliography

2. The λ-Calculus as a Programming Language

3. Functional Programming in Haskell

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.2/16

1. Bibliography

1. Haskell: the Craft of Functional Programming, S Thompson, 2nd Edition,

Addison-Wesley, 1999. (available @ QML)

2. Introduction to Functional Programming using Haskell, Richard Bird, 2nd

Edition, Prentice Hall, 1998.

3. Concepts of Programming Languages, R Sebesta, 6th Ed., Addison-Wesley,

2004. (available @ QML)

4. Comparative Programming Languages, R Clark, 3rd Ed, Addison-Wesley,

2000. (available @ QML)

5. Programming Languages, Paradigm and Practice, D Appleby & J Van de

Kopple, 2nd. Ed., Mcgraw-Hill, 1997.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.3/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2. The λ-Calculus as a Programming Language

• The λ-calculus can be seen as a simple programming language.

• Advantages:

1. It has a simple syntax and a precise semantics;

2. Computation is defined as the application of just three rules;

3. It is free from “architectural” influences;

4. Its mathematical foundations enable proofs over programs.

• λ-calculus: formalise semantics of other programming languages

• λ-calculus: basis for functional programming.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.4/16

2.1 From λ-Calculus to Functional Programming

• Disadvantages of the λ-calculus:

• Programs written as λ-expressions can be too long;

• It may be hard to follow all the substitutions.

• Solution: higher-level language on top of the λ-calculus.

• Resulting class: functional programming languages.

• Functional programming = λ-calculus + “syntactic sugar”

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.5/16

2.1 From λ-Calculus to Functional Programming

• Disadvantages of the λ-calculus:

• Programs written as λ-expressions can be too long;

• It may be hard to follow all the substitutions.

• Solution: higher-level language on top of the λ-calculus.

• Resulting class: functional programming languages.

• Functional programming = λ-calculus + “syntactic sugar”

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.5/16

2.1 From λ-Calculus to Functional Programming

• Disadvantages of the λ-calculus:

• Programs written as λ-expressions can be too long;

• It may be hard to follow all the substitutions.

• Solution: higher-level language on top of the λ-calculus.

• Resulting class: functional programming languages.

• Functional programming = λ-calculus + “syntactic sugar”

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.5/16

2.1 From λ-Calculus to Functional Programming

• Disadvantages of the λ-calculus:

• Programs written as λ-expressions can be too long;

• It may be hard to follow all the substitutions.

• Solution: higher-level language on top of the λ-calculus.

• Resulting class: functional programming languages.

• Functional programming = λ-calculus + “syntactic sugar”

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.5/16

2.1 From λ-Calculus to Functional Programming

• Disadvantages of the λ-calculus:

• Programs written as λ-expressions can be too long;

• It may be hard to follow all the substitutions.

• Solution: higher-level language on top of the λ-calculus.

• Resulting class: functional programming languages.

• Functional programming = λ-calculus + “syntactic sugar”

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.5/16

2.1 From λ-Calculus to Functional Programming

• Disadvantages of the λ-calculus:

• Programs written as λ-expressions can be too long;

• It may be hard to follow all the substitutions.

• Solution: higher-level language on top of the λ-calculus.

• Resulting class: functional programming languages.

• Functional programming = λ-calculus + “syntactic sugar”

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.5/16

2.2 Some Functional Programming Languages

(Lisp M.I.T (USA) 1960)

ML University of Edinburgh 1978

SASL University of St. Andrews 1976

Hope University of Edinburgh 1980

SISAL Lawrence Livermore National Lab. (USA) 1983

Clean University of Nijmegen (NL) 1984

Haskell University of Kent at Canterbury 1985

Haskell Chalmers University (SE), Yale University 1987

(USA) and University of Glasgow

Jython (Java) CNRI 1997

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.6/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3. Functional Programming in Haskell

• In Haskell, f(x) is “f x”. (compare with λx.fx)

• Haskell’s main programming concept is function application:

sin (square z) maximum (abs -5) (abs 3)

• Function application: higher precedence than anything else

1 + f x means 1 + (f x) not (1 + f) x

f x - 3 means (f x) - 3 not f (x - 3)

• N.B.: Precedence even over operators such as “+” and “*”!

• Function application is left associative:

f g x means (f g) x not f (g x)f g h x means ((f g) h) x not f (g (h x))

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.7/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.1 Variables in Haskell

• Similar to variables in mathematical equations

• Not a pointer to a memory cell!!

• Place-holder for unknown values

• Local to their particular function definition

• A variable receives a value when a function is applied to its arguments

• No other way to assign a value to a variable

• No destructive assignment: not possible to alter value of variable

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.8/16

3.2 Defining Functions in Haskell

• Function definitions in “equational” style, e.g.

a = 42.4

f x = x + a

g x y = x

poly a b c x = (a * x ˆ 2) + (b * x) + c

• Haskell requires us to name each function definition explicitly

• Variable: any unquoted string not used as a function name

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.9/16

3.2 Defining Functions in Haskell

• Function definitions in “equational” style, e.g.

a = 42.4

f x = x + a

g x y = x

poly a b c x = (a * x ˆ 2) + (b * x) + c

• Haskell requires us to name each function definition explicitly

• Variable: any unquoted string not used as a function name

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.9/16

3.2 Defining Functions in Haskell

• Function definitions in “equational” style, e.g.

a = 42.4

f x = x + a

g x y = x

poly a b c x = (a * x ˆ 2) + (b * x) + c

• Haskell requires us to name each function definition explicitly

• Variable: any unquoted string not used as a function name

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.9/16

3.3 Using Functions in Haskell

• After functions are defined as previously, we can run them.

• Edit functions in file (use extension .hs, as in myprog.hs)

• Load file with definitions in Haskell interpreter:

• Check course web-page for downloads.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.10/16

3.3 Using Functions in Haskell

• After functions are defined as previously, we can run them.

• Edit functions in file (use extension .hs, as in myprog.hs)

• Load file with definitions in Haskell interpreter:

• Check course web-page for downloads.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.10/16

3.3 Using Functions in Haskell

• After functions are defined as previously, we can run them.

• Edit functions in file (use extension .hs, as in myprog.hs)

• Load file with definitions in Haskell interpreter:

• Check course web-page for downloads.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.10/16

3.3 Using Functions in Haskell

• After functions are defined as previously, we can run them.

• Edit functions in file (use extension .hs, as in myprog.hs)

• Load file with definitions in Haskell interpreter:

• Check course web-page for downloads.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.10/16

3.4 Function Definition by Cases (1)

• Haskell allows the definition of functions using guards:

relative a b| a > b = "higher"| a == b = "equal"| a < b = "lower"

• N.B.: Make sure conditions for each case are mutually exclusive.

• Compiler does not check conditions!

• To help us, Haskell provides the “otherwise” keyword:

relative a b| a > b = "higher"| a == b = "equal"| otherwise = "lower"

• Haskell selects the first alternative that holds (top-to-bottom).

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.11/16

3.4 Function Definition by Cases (1)

• Haskell allows the definition of functions using guards:

relative a b| a > b = "higher"| a == b = "equal"| a < b = "lower"

• N.B.: Make sure conditions for each case are mutually exclusive.

• Compiler does not check conditions!

• To help us, Haskell provides the “otherwise” keyword:

relative a b| a > b = "higher"| a == b = "equal"| otherwise = "lower"

• Haskell selects the first alternative that holds (top-to-bottom).

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.11/16

3.4 Function Definition by Cases (1)

• Haskell allows the definition of functions using guards:

relative a b| a > b = "higher"| a == b = "equal"| a < b = "lower"

• N.B.: Make sure conditions for each case are mutually exclusive.

• Compiler does not check conditions!

• To help us, Haskell provides the “otherwise” keyword:

relative a b| a > b = "higher"| a == b = "equal"| otherwise = "lower"

• Haskell selects the first alternative that holds (top-to-bottom).

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.11/16

3.4 Function Definition by Cases (1)

• Haskell allows the definition of functions using guards:

relative a b| a > b = "higher"| a == b = "equal"| a < b = "lower"

• N.B.: Make sure conditions for each case are mutually exclusive.

• Compiler does not check conditions!

• To help us, Haskell provides the “otherwise” keyword:

relative a b| a > b = "higher"| a == b = "equal"| otherwise = "lower"

• Haskell selects the first alternative that holds (top-to-bottom).

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.11/16

3.4 Function Definition with Patterns (1)

• Important syntactic sugar: arguments as patterns:

cave man 0 = "none"cave man 1 = "one"cave man n = "lots"

• Equivalent function with guards:

cave man n| n == 0 = "none"| n == 1 = "one"| otherwise = "many"

• It is good practice to use the patterned form of function definition, where

possible.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.12/16

3.4 Function Definition with Patterns (1)

• Important syntactic sugar: arguments as patterns:

cave man 0 = "none"cave man 1 = "one"cave man n = "lots"

• Equivalent function with guards:

cave man n| n == 0 = "none"| n == 1 = "one"| otherwise = "many"

• It is good practice to use the patterned form of function definition, where

possible.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.12/16

3.4 Function Definition with Patterns (1)

• Important syntactic sugar: arguments as patterns:

cave man 0 = "none"cave man 1 = "one"cave man n = "lots"

• Equivalent function with guards:

cave man n| n == 0 = "none"| n == 1 = "one"| otherwise = "many"

• It is good practice to use the patterned form of function definition, where

possible.

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.12/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.4 Function Definition with Patterns (2)

• Ordering of cases is essential when patterns are not mutually exclusive!

• Haskell selects the first case whose pattern matches the value of the

arguments:

right: fn a 3 = True wrong: fn a b = Falsefn a b = False fn a 3 = True

• Rule of thumb: most specific cases first!

• A useful pattern for functions over natural numbers is “(n+k)”

– n is a variable and k is a positive number like 1 (not a variable):

– Example: predecessor (n+1) = n

– A given x matches (n+k) iff x is an integer greater than k

– n takes on the value x−k: predecessor 5 4

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.13/16

3.5 Defining Local Functions in Haskell

• Define a function local to another function’s definition:

calc x y| x > y = 2 * a/b| x == y = 0| otherwise = 2 * b/awhere a = x + y

b = x - y

sum cubes x y z = vol x + vol y + vol zwhere vol c = c * c * c

sum cylinders x y z = vol x + vol y + vol zwhere vol c = pi * c * c

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.14/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.6 The Offside Rule for Function Definitions

• Haskell allows the programmer a lot of freedom in laying out programs.

• Layout: for the benefit of humans, to improve visualisation.

• However, there are situations when layout is significant: offside rule

“the right hand side of an equation is terminated by any text

appearing to the left of its first character or at the same indentation

(offside)”

• Example:

expensive p = (p > 100)

vat p = 17.5 *

p/100

• Function expensive ends just before the “v” of vat

• The definition of vat is offside and will cause an error!CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.15/16

3.7 Recommended Layout

• A recommended layout is:

f p1 p2 ... pk

| cond1 = res1

| cond2 = res2

...

| otherwise = resm

where

v1 a1 a2 ... an = sol1

v2 = sol2

...

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.16/16

3.7 Recommended Layout

• A recommended layout is:

f p1 p2 ... pk

| cond1 = res1

| cond2 = res2

...

| otherwise = resm

where

v1 a1 a2 ... an = sol1

v2 = sol2

...

CS4026-I(3) (Introduction to Haskell), Dept. of Computing Science, Univ. of Aberdeen, 2006–2007 – p.16/16