Lecture 1 - Introduction
Lecture 1 - Introduction
Functional
Program-
ming
Michael R. Hansen
1 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
02157
Functional
Program-
ming
Michael R. Ha
WELCOME to
02157 Functional Programming
Homepage: [Link]/courses/02157
2 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Today: Friday, September 7.
02157
Functional
Program-
ming
Michael R. Ha
• Introduction to functional programming and F#
(341.23 — here)
• about 9:15 – lecture notes can be bought here.
• Make your first programs in the databar
(341 Rooms: 015 and 019 — E-databar)
• Introduction to lists in F#
(341.23 — here)
• Computations with polynomials in F#
(341 Rooms: 015 and 019 — E-databar)
3 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Today: Friday, September 7.
02157
Functional
Program-
ming
Michael R. Ha
• Introduction to functional programming and F#
(341.23 — here)
• about 9:15 – lecture notes can be bought here.
• Make your first programs in the databar
(341 Rooms: 015 and 019 — E-databar)
• Introduction to lists in F#
(341.23 — here)
• Computations with polynomials in F#
(341 Rooms: 015 and 019 — E-databar)
4 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Practical Matters
02157
Functional
Program-
ming
• Textbook: Functional Programming using F#, Chapters 1-7, 9.
Michael R. Ha
by Michael R. Hansen and Hans Rischel.
Can be bought at the reception of DTU Informatics. Price 100 kr.
Published by Cambridge University Press the coming winter.
5 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Practical Matters
02157
Functional
Program-
ming
• Textbook: Functional Programming using F#, Chapters 1-7, 9.
Michael R. Ha
by Michael R. Hansen and Hans Rischel.
Can be bought at the reception of DTU Informatics. Price 100 kr.
Published by Cambridge University Press the coming winter.
6 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Practical Matters
02157
Functional
Program-
ming
• Textbook: Functional Programming using F#, Chapters 1-7, 9.
Michael R. Ha
by Michael R. Hansen and Hans Rischel.
Can be bought at the reception of DTU Informatics. Price 100 kr.
Published by Cambridge University Press the coming winter.
7 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Practical Matters
02157
Functional
Program-
ming
• Textbook: Functional Programming using F#, Chapters 1-7, 9.
Michael R. Ha
by Michael R. Hansen and Hans Rischel.
Can be bought at the reception of DTU Informatics. Price 100 kr.
Published by Cambridge University Press the coming winter.
8 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Imperative models
02157
Functional
Program-
ming
Michael R. Ha
• Imperative models of computations are expressed in terms of
states and sequences of state-changing operations
Example:
i := 0;
s := 0;
while i < length(A)
do s := s+A[i];
i := i+1
od
9 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Imperative models
02157
Functional
Program-
ming
Michael R. Ha
• Imperative models of computations are expressed in terms of
states and sequences of state-changing operations
Example:
i := 0;
s := 0;
while i < length(A)
do s := s+A[i];
i := i+1
od
10 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Object-oriented models
02157
Functional
Program-
ming
Michael R. Ha
11 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Object-oriented models
02157
Functional
Program-
ming
Michael R. Ha
12 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Declarative models
• Functional programming
• A program is expressed as a mathematical function
f :A→B
and function applications guide computations.
Some advantages
• fast prototyping based on abstract concepts
• more advanced applications are within reach
• Supplement modelling and problem solving techniques
• Execute in parallel on multi-core platforms
F# is as efficient as C#
13 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Declarative models
• Functional programming
• A program is expressed as a mathematical function
f :A→B
and function applications guide computations.
Some advantages
• fast prototyping based on abstract concepts
• more advanced applications are within reach
• Supplement modelling and problem solving techniques
• Execute in parallel on multi-core platforms
F# is as efficient as C#
14 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Declarative models
• Functional programming
• A program is expressed as a mathematical function
f :A→B
and function applications guide computations.
Some advantages
• fast prototyping based on abstract concepts
• more advanced applications are within reach
• Supplement modelling and problem solving techniques
• Execute in parallel on multi-core platforms
F# is as efficient as C#
15 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some functional programming background
02157
Functional
In functional programming, the model of computation is the Program-
ming
application of functions to arguments. no side-effects Michael R. Ha
16 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some functional programming background
02157
Functional
In functional programming, the model of computation is the Program-
ming
application of functions to arguments. no side-effects Michael R. Ha
17 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some functional programming background
02157
Functional
In functional programming, the model of computation is the Program-
ming
application of functions to arguments. no side-effects Michael R. Ha
18 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some functional programming background
02157
Functional
In functional programming, the model of computation is the Program-
ming
application of functions to arguments. no side-effects Michael R. Ha
19 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some background of the “SML-family”
02157
• Standard Meta Language (SML) was originally designed for Functional
Program-
theorem proving ming
20 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some background of the “SML-family”
02157
• Standard Meta Language (SML) was originally designed for Functional
Program-
theorem proving ming
21 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some background of the “SML-family”
02157
• Standard Meta Language (SML) was originally designed for Functional
Program-
theorem proving ming
22 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some background of the “SML-family”
02157
• Standard Meta Language (SML) was originally designed for Functional
Program-
theorem proving ming
23 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some background of the “SML-family”
02157
• Standard Meta Language (SML) was originally designed for Functional
Program-
theorem proving ming
24 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Some background of the “SML-family”
02157
• Standard Meta Language (SML) was originally designed for Functional
Program-
theorem proving ming
25 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview of the course
02157
Functional
Program-
• Functional programming concepts and techniques ming
26 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview of the course
02157
Functional
Program-
• Functional programming concepts and techniques ming
27 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview of the course
02157
Functional
Program-
• Functional programming concepts and techniques ming
28 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
A major goal
02157
Functional
Teach abstraction (not a concrete programming language) Program-
ming
• Modelling
Michael R. Ha
• Design
• Programming
Why?
More complex problems can be solved in an succinct, elegant and
understandable manner
How?
Solving a broad class of problems showing the applicability of the
theory, concepts, techniques and tools.
29 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
A major goal
02157
Functional
Teach abstraction (not a concrete programming language) Program-
ming
• Modelling
Michael R. Ha
• Design
• Programming
Why?
More complex problems can be solved in an succinct, elegant and
understandable manner
How?
Solving a broad class of problems showing the applicability of the
theory, concepts, techniques and tools.
30 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
A major goal
02157
Functional
Teach abstraction (not a concrete programming language) Program-
ming
• Modelling
Michael R. Ha
• Design
• Programming
Why?
More complex problems can be solved in an succinct, elegant and
understandable manner
How?
Solving a broad class of problems showing the applicability of the
theory, concepts, techniques and tools.
31 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
A major goal
02157
Functional
Teach abstraction (not a concrete programming language) Program-
ming
• Modelling
Michael R. Ha
• Design
• Programming
Why?
More complex problems can be solved in an succinct, elegant and
understandable manner
How?
Solving a broad class of problems showing the applicability of the
theory, concepts, techniques and tools.
32 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
F# supports
02157
Functional
Program-
ming
• Functions as first class citizens Michael R. Ha
33 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
F# supports
02157
Functional
Program-
ming
• Functions as first class citizens Michael R. Ha
34 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
F# supports
02157
Functional
Program-
ming
• Functions as first class citizens Michael R. Ha
35 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
F# supports
02157
Functional
Program-
ming
• Functions as first class citizens Michael R. Ha
36 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
F# supports
02157
Functional
Program-
ming
• Functions as first class citizens Michael R. Ha
37 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Course context
02157
Functional
Prerequisites for 02157: Programming in an Program-
imperative/object-oriented language, discrete mathematics, ming
algorithms and data structure, as obtained, for example, from the Michael R. Ha
38 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Course context
02157
Functional
Prerequisites for 02157: Programming in an Program-
imperative/object-oriented language, discrete mathematics, ming
algorithms and data structure, as obtained, for example, from the Michael R. Ha
39 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview of Getting Started
02157
Functional
Program-
Main functional ingredients of F#: ming
GOAL: By the end of this first part you have constructed succinct,
elegant and understandable F# programs, e.g. for
Pn
• sum(m, n) = i=m i
40 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview of Getting Started
02157
Functional
Program-
Main functional ingredients of F#: ming
GOAL: By the end of this first part you have constructed succinct,
elegant and understandable F# programs, e.g. for
Pn
• sum(m, n) = i=m i
41 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
The Interactive Environment
02157
Functional
Program-
ming
42 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
The Interactive Environment
02157
Functional
Program-
ming
43 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
The Interactive Environment
02157
Functional
Program-
ming
44 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
The Interactive Environment
02157
Functional
Program-
ming
45 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Value Declarations
02157
Functional
Program-
ming
A value declaration has the form: let identifier = expression
Michael R. Ha
46 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Value Declarations
02157
Functional
Program-
ming
A value declaration has the form: let identifier = expression
Michael R. Ha
47 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Value Declarations
02157
Functional
Program-
ming
A value declaration has the form: let identifier = expression
Michael R. Ha
48 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Function Declarations 1: let f x = e
02157
Functional
Declaration of the circle area function: Program-
ming
let circleArea r = [Link] * r * r;; Michael R. Ha
49 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Function Declarations 1: let f x = e
02157
Functional
Declaration of the circle area function: Program-
ming
let circleArea r = [Link] * r * r;; Michael R. Ha
50 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Function Declarations 1: let f x = e
02157
Functional
Declaration of the circle area function: Program-
ming
let circleArea r = [Link] * r * r;; Michael R. Ha
51 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Anonymous functions: by example (1)
02157
Functional
Program-
One wildcard pattern can cover many similar cases: ming
Michael R. Ha
function
| 2 -> 28 // February
| 4 -> 30 // April
| 6 -> 30 // June
| 9 -> 30 // September
| 11 -> 30 // November
| _ -> 31;;// All other months
55 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Anonymous functions: by example (2)
02157
Functional
Program-
One wildcard pattern can cover many similar cases: ming
Michael R. Ha
function
| 2 -> 28 // February
| 4 -> 30 // April
| 6 -> 30 // June
| 9 -> 30 // September
| 11 -> 30 // November
| _ -> 31;;// All other months
56 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion. Example n! = 1 · 2 · . . . · n, n ≥ 0
02157
Functional
Program-
ming
Michael R. Ha
Mathematical definition: recursion formula
0! = 1 (i)
n! = n · (n − 1)!, for n > 0 (ii)
Computation:
3!
= 3 · (3 − 1)! (ii)
= 3 · 2 · (2 − 1)! (ii)
= 3 · 2 · 1 · (1 − 1)! (ii)
= 3·2·1·1 (i)
= 6
57 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion. Example n! = 1 · 2 · . . . · n, n ≥ 0
02157
Functional
Program-
ming
Michael R. Ha
Mathematical definition: recursion formula
0! = 1 (i)
n! = n · (n − 1)!, for n > 0 (ii)
Computation:
3!
= 3 · (3 − 1)! (ii)
= 3 · 2 · (2 − 1)! (ii)
= 3 · 2 · 1 · (1 − 1)! (ii)
= 3·2·1·1 (i)
= 6
58 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursive declaration. Example n!
02157
Functional
Program-
ming
Function declaration:
Michael R. Ha
e1 e2 reads: e1 evaluates to e2
59 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursive declaration. Example n!
02157
Functional
Program-
ming
Function declaration:
Michael R. Ha
e1 e2 reads: e1 evaluates to e2
60 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursive declaration. Example n!
02157
Functional
Program-
ming
Function declaration:
Michael R. Ha
e1 e2 reads: e1 evaluates to e2
61 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursive declaration. Example n!
02157
Functional
Program-
ming
Function declaration:
Michael R. Ha
e1 e2 reads: e1 evaluates to e2
62 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursive declaration. Example n!
02157
Functional
Program-
ming
Function declaration:
Michael R. Ha
e1 e2 reads: e1 evaluates to e2
63 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursive declaration. Example n!
02157
Functional
Program-
ming
Function declaration:
Michael R. Ha
e1 e2 reads: e1 evaluates to e2
64 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion. Example x n = x · . . . · x, n occurrences of x
02157
Mathematical definition: recursion formula Functional
Program-
x0 = 1 (1) ming
xn x · x n−1 ,
Michael R. Ha
= for n > 0 (2)
Function declaration:
let rec power = function
| ( ,0) -> 1.0 (* 1 *)
| (x,n) -> x * power(x,n-1) (* 2 *)
Patterns:
( , 0) matches any pair of the form (x, 0).
The wildcard pattern matches any value.
(x, n) matches any pair (u, i) yielding the bindings
x 7→ u, n 7→ i
65 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion. Example x n = x · . . . · x, n occurrences of x
02157
Mathematical definition: recursion formula Functional
Program-
x0 = 1 (1) ming
xn x · x n−1 ,
Michael R. Ha
= for n > 0 (2)
Function declaration:
let rec power = function
| ( ,0) -> 1.0 (* 1 *)
| (x,n) -> x * power(x,n-1) (* 2 *)
Patterns:
( , 0) matches any pair of the form (x, 0).
The wildcard pattern matches any value.
(x, n) matches any pair (u, i) yielding the bindings
x 7→ u, n 7→ i
66 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion. Example x n = x · . . . · x, n occurrences of x
02157
Mathematical definition: recursion formula Functional
Program-
x0 = 1 (1) ming
xn x · x n−1 ,
Michael R. Ha
= for n > 0 (2)
Function declaration:
let rec power = function
| ( ,0) -> 1.0 (* 1 *)
| (x,n) -> x * power(x,n-1) (* 2 *)
Patterns:
( , 0) matches any pair of the form (x, 0).
The wildcard pattern matches any value.
(x, n) matches any pair (u, i) yielding the bindings
x 7→ u, n 7→ i
67 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Evaluation. Example: power(4.0, 2)
02157
Functional
Program-
ming
Function declaration: Michael R. Ha
Evaluation:
power(4.0, 2)
4.0 ∗ power(4.0, 2 − 1) Clause 2, [x 7→ 4.0, n 7→ 2]
4.0 ∗ power(4.0, 1)
4.0 ∗ (4.0 ∗ power(4.0, 1 − 1)) Clause 2, [x 7→ 4.0, n 7→ 1]
4.0 ∗ (4.0 ∗ power(4.0, 0))
4.0 ∗ (4.0 ∗ 1) Clause 1
16.0
68 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
If-then-else expressions
02157
Form: Functional
Program-
if b then e1 else e2 ming
Michael R. Ha
Evaluation rules:
if true then e1 else e2 e1
if false then e1 else e2 e2
Alternative declarations:
let rec fact n = if n=0 then 1
else n * fact(n-1);
69 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
If-then-else expressions
02157
Form: Functional
Program-
if b then e1 else e2 ming
Michael R. Ha
Evaluation rules:
if true then e1 else e2 e1
if false then e1 else e2 e2
Alternative declarations:
let rec fact n = if n=0 then 1
else n * fact(n-1);
70 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
If-then-else expressions
02157
Form: Functional
Program-
if b then e1 else e2 ming
Michael R. Ha
Evaluation rules:
if true then e1 else e2 e1
if false then e1 else e2 e2
Alternative declarations:
let rec fact n = if n=0 then 1
else n * fact(n-1);
71 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Booleans
02157
Functional
Program-
Type name bool ming
Michael R. Ha
Values false, true
Expressions
e1 && e2 “conjunction e1 ∧ e2 ”
e1 || e2 “disjunction e1 ∨ e2 ”
1<2 || 5/0 = 1
— are lazily evaluated, e.g.
true
72 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Booleans
02157
Functional
Program-
Type name bool ming
Michael R. Ha
Values false, true
Expressions
e1 && e2 “conjunction e1 ∧ e2 ”
e1 || e2 “disjunction e1 ∨ e2 ”
1<2 || 5/0 = 1
— are lazily evaluated, e.g.
true
73 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Booleans
02157
Functional
Program-
Type name bool ming
Michael R. Ha
Values false, true
Expressions
e1 && e2 “conjunction e1 ∧ e2 ”
e1 || e2 “disjunction e1 ∨ e2 ”
1<2 || 5/0 = 1
— are lazily evaluated, e.g.
true
74 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Strings
02157
Functional
Program-
Type name string ming
Michael R. Ha
Values "abcd", " ", "", "123\"321" (escape sequence for ")
Operator Type
[Link] string -> int length of string
+ string*string -> string concatenation
= < <= ... string*string -> bool comparisons
string obj -> string conversions
Examples
- "abc"+"de"; - string(6+18);
> val it = "abcde": string > val it = "24": string
75 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Strings
02157
Functional
Program-
Type name string ming
Michael R. Ha
Values "abcd", " ", "", "123\"321" (escape sequence for ")
Operator Type
[Link] string -> int length of string
+ string*string -> string concatenation
= < <= ... string*string -> bool comparisons
string obj -> string conversions
Examples
- "abc"+"de"; - string(6+18);
> val it = "abcde": string > val it = "24": string
76 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Strings
02157
Functional
Program-
Type name string ming
Michael R. Ha
Values "abcd", " ", "", "123\"321" (escape sequence for ")
Operator Type
[Link] string -> int length of string
+ string*string -> string concatenation
= < <= ... string*string -> bool comparisons
string obj -> string conversions
Examples
- "abc"+"de"; - string(6+18);
> val it = "abcde": string > val it = "24": string
77 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Types — every expression has a type e : τ
02157
Functional
Basic types: Program-
ming
type name example of values
Michael R. Ha
Integers int ˜27, 0, 15, 21000
Floats float ˜27.3, 0.0, 48.21
Booleans bool true, false
Pairs:
If e1 : τ1 and e2 : τ2
then (e1 , e2 ) : τ1 ∗τ2 pair (tuple) type constructor
Functions:
if f : τ1 -> τ2 and a : τ1 function type constructor
then f (a) : τ2
Examples:
(4.0, 2): float*int
power: float*int -> float * has higher precedence that ->
power(4.0, 2): float
78 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Types — every expression has a type e : τ
02157
Functional
Basic types: Program-
ming
type name example of values
Michael R. Ha
Integers int ˜27, 0, 15, 21000
Floats float ˜27.3, 0.0, 48.21
Booleans bool true, false
Pairs:
If e1 : τ1 and e2 : τ2
then (e1 , e2 ) : τ1 ∗τ2 pair (tuple) type constructor
Functions:
if f : τ1 -> τ2 and a : τ1 function type constructor
then f (a) : τ2
Examples:
(4.0, 2): float*int
power: float*int -> float * has higher precedence that ->
power(4.0, 2): float
79 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Types — every expression has a type e : τ
02157
Functional
Basic types: Program-
ming
type name example of values
Michael R. Ha
Integers int ˜27, 0, 15, 21000
Floats float ˜27.3, 0.0, 48.21
Booleans bool true, false
Pairs:
If e1 : τ1 and e2 : τ2
then (e1 , e2 ) : τ1 ∗τ2 pair (tuple) type constructor
Functions:
if f : τ1 -> τ2 and a : τ1 function type constructor
then f (a) : τ2
Examples:
(4.0, 2): float*int
power: float*int -> float * has higher precedence that ->
power(4.0, 2): float
80 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Types — every expression has a type e : τ
02157
Functional
Basic types: Program-
ming
type name example of values
Michael R. Ha
Integers int ˜27, 0, 15, 21000
Floats float ˜27.3, 0.0, 48.21
Booleans bool true, false
Pairs:
If e1 : τ1 and e2 : τ2
then (e1 , e2 ) : τ1 ∗τ2 pair (tuple) type constructor
Functions:
if f : τ1 -> τ2 and a : τ1 function type constructor
then f (a) : τ2
Examples:
(4.0, 2): float*int
power: float*int -> float * has higher precedence that ->
power(4.0, 2): float
81 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
82 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
83 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
84 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
85 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
86 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
87 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
88 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Type inference: power
02157
Functional
Program-
let rec power = function ming
89 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Summary
02157
Functional
Program-
ming
Michael R. Ha
90 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Summary
02157
Functional
Program-
ming
Michael R. Ha
91 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview
02157
Functional
Program-
ming
Michael R. Ha
92 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Overview
02157
Functional
Program-
ming
Michael R. Ha
93 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Lists
02157
A list is a finite sequence of elements having the same type: Functional
Program-
ming
[v1 ; . . . ; vn ] ([ ] is called the empty list) Michael R. Ha
[2;3;6];;
val it : int list = [2; 3; 6]
[sin; cos];;
val it : (float->float) list = [<fun:...>; <fun:...>]
[(1,true); (3,true)];;
val it : (int * bool) list = [(1, true); (3, true)]
94 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Lists
02157
A list is a finite sequence of elements having the same type: Functional
Program-
ming
[v1 ; . . . ; vn ] ([ ] is called the empty list) Michael R. Ha
[2;3;6];;
val it : int list = [2; 3; 6]
[sin; cos];;
val it : (float->float) list = [<fun:...>; <fun:...>]
[(1,true); (3,true)];;
val it : (int * bool) list = [(1, true); (3, true)]
95 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Lists
02157
A list is a finite sequence of elements having the same type: Functional
Program-
ming
[v1 ; . . . ; vn ] ([ ] is called the empty list) Michael R. Ha
[2;3;6];;
val it : int list = [2; 3; 6]
[sin; cos];;
val it : (float->float) list = [<fun:...>; <fun:...>]
[(1,true); (3,true)];;
val it : (int * bool) list = [(1, true); (3, true)]
96 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Lists
02157
A list is a finite sequence of elements having the same type: Functional
Program-
ming
[v1 ; . . . ; vn ] ([ ] is called the empty list) Michael R. Ha
[2;3;6];;
val it : int list = [2; 3; 6]
[sin; cos];;
val it : (float->float) list = [<fun:...>; <fun:...>]
[(1,true); (3,true)];;
val it : (int * bool) list = [(1, true); (3, true)]
97 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Lists
02157
A list is a finite sequence of elements having the same type: Functional
Program-
ming
[v1 ; . . . ; vn ] ([ ] is called the empty list) Michael R. Ha
[2;3;6];;
val it : int list = [2; 3; 6]
[sin; cos];;
val it : (float->float) list = [<fun:...>; <fun:...>]
[(1,true); (3,true)];;
val it : (int * bool) list = [(1, true); (3, true)]
98 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Lists
02157
A list is a finite sequence of elements having the same type: Functional
Program-
ming
[v1 ; . . . ; vn ] ([ ] is called the empty list) Michael R. Ha
[2;3;6];;
val it : int list = [2; 3; 6]
[sin; cos];;
val it : (float->float) list = [<fun:...>; <fun:...>]
[(1,true); (3,true)];;
val it : (int * bool) list = [(1, true); (3, true)]
99 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Trees for lists
::
@
@
@
2 ::
@
@
@
3 :: ::
@ @
@ @
@ @
2 [] 2 []
100 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Trees for lists
::
@
@
@
2 ::
@
@
@
3 :: ::
@ @
@ @
@ @
2 [] 2 []
101 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
List constructors: [] and ::
02157
Lists are generated as follows: Functional
Program-
• the empty list is a list, designated [] ming
Michael R. Ha
• if x is an element and xs is a list,
then so is x :: xs (type consistency)
::
@
@
@
x1 ::
@
@
@
x2 xs
102 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
List constructors: [] and ::
02157
Lists are generated as follows: Functional
Program-
• the empty list is a list, designated [] ming
Michael R. Ha
• if x is an element and xs is a list,
then so is x :: xs (type consistency)
::
@
@
@
x1 ::
@
@
@
x2 xs
103 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion on lists – a simple example
02157
n
X n
X Functional
suml [x1 ,x2 , . . . ,xn ] = xi = x1 + x2 + · · · + xn = x1 + xi Program-
ming
i=1 i=2
Michael R. Ha
suml [1;2]
1 + suml [2] (x 7→ 1 and xs 7→ [2])
1 + (2 + suml []) (x 7→ 2 and xs 7→ [])
1 + (2 + 0) (the pattern [] matches the value [])
1 + 2
3
104 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion on lists – a simple example
02157
n
X n
X Functional
suml [x1 ,x2 , . . . ,xn ] = xi = x1 + x2 + · · · + xn = x1 + xi Program-
ming
i=1 i=2
Michael R. Ha
suml [1;2]
1 + suml [2] (x 7→ 1 and xs 7→ [2])
1 + (2 + suml []) (x 7→ 2 and xs 7→ [])
1 + (2 + 0) (the pattern [] matches the value [])
1 + 2
3
105 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Recursion on lists – a simple example
02157
n
X n
X Functional
suml [x1 ,x2 , . . . ,xn ] = xi = x1 + x2 + · · · + xn = x1 + xi Program-
ming
i=1 i=2
Michael R. Ha
suml [1;2]
1 + suml [2] (x 7→ 1 and xs 7→ [2])
1 + (2 + suml []) (x 7→ 2 and xs 7→ [])
1 + (2 + 0) (the pattern [] matches the value [])
1 + 2
3
106 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Infix functions
It is possible to declare infix functions in F#, i.e. the function symbol 02157
Functional
is between the arguments. Program-
ming
It is possible to declare infix functions in F#, i.e. the function symbol 02157
Functional
is between the arguments. Program-
ming
It is possible to declare infix functions in F#, i.e. the function symbol 02157
Functional
is between the arguments. Program-
ming
It is possible to declare infix functions in F#, i.e. the function symbol 02157
Functional
is between the arguments. Program-
ming
02157
Functional
Program-
ming
Michael R. Ha
111 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012
Exercises
02157
Functional
Program-
ming
Michael R. Ha
112 DTU Informatics, Technical University of Denmark Lecture 1: Introduction and Getting Started MRH 6/09/2012