Professional Documents
Culture Documents
Intro To CS
Intro To CS
Home Page
Title Page
S. Arun-Kumar JJ II
sak@cse.iitd.ernet.in J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
1. Computing: The Functional Way
1.1. Introduction to Computing
1. Introduction Computing: The . . .
Algorithms: Design & . . .
2. Computing tools
Introducing Reals
5. Primitives Imperative . . .
A large Example: . . .
6. Algorithm Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Page 2 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Computing tools
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
J I
• slide rules (ask a retired engineer!) Page 4 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ruler and Compass
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
• Bisect an angle JJ II
√ Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing and
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Computers
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
computer too! J I
Go Back
besides computing. Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Primitives
• Each tool has a set of capabilities Computing: The . . .
Algorithms: Design & . . .
JJ II
tion. Go Back
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Title Page
rithm Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
1.2. Our Computing Tool
Computing: The . . .
1. The Digital Computer: Our Computing Tool
Algorithms: Design & . . .
2. Algorithms Introducing Reals
Correctness, . . .
3. Programming Language
Compound Data
4. Programs and Languages Higher Order Functions . . .
Imperative . . .
5. Programs
A large Example: . . .
6. Programming Lecture-wise Index to . . .
8. Primitives
Title Page
9. Primitive expressions
JJ II
10. Methods of combination
11. Methods of abstraction J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. A Computation: Factorial
19. A Computation: Factorial
20. A Computation: Factorial
Computing: The . . .
Home Page
Title Page
JJ II
J I
Page 10 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Digital Computer:
Our Computing Tool Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
put JJ II
J I
• It is unambiguous Page 11 of 644
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithms
An algorithm will be written in a mix- Computing: The . . .
Algorithms: Design & . . .
Title Page
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programming
Language Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
J I
chine
Page 13 of 644
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programs and
Languages Computing: The . . .
Algorithms: Design & . . .
Title Page
lowing rigid grammatical rules
JJ II
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programs Computing: The . . .
Title Page
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Programming
Computing: The . . .
The act of writing programs and test- Algorithms: Design & . . .
Introducing Reals
J I
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing Models
We consider mainly two models. Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
by a sequence of commands to be JJ II
executed. J I
Page 17 of 644
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Primitives Computing: The . . .
Algorithms: Design & . . .
Title Page
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Primitive expressions
The simplest objects and operations Computing: The . . .
clude Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
Page 19 of 644
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Methods of Correctness, . . .
Compound Data
J I
Examples: composition of functions, Page 20 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
unit JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Functional Model
The functional model is very conve- Computing: The . . .
Algorithms: Design & . . .
Title Page
tor JJ II
Go Back
Standard ML Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Mathematical Notation
Compound Data
Higher Order Functions . . .
Imperative . . .
Home Page
Title Page
1 if n < 1
n! = JJ II
n × (n − 1)! otherwise J I
Page 23 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
2: Factorial
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
1 if n < 1 JJ II
n! =
1 × 2 × . . . × n otherwise J I
Page 24 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Mathematical Notation
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
1 if n < 1 Title Page
n! =
(n + 1)!/(n + 1) otherwise JJ II
J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
Title Page
Page 26 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
J I
-
Page 27 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
sml Title Page
- fun fact n = J I
= Page 28 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
A Computation: Correctness, . . .
Compound Data
Higher Order Functions . . .
Factorial Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
- fun fact n = JJ II
J I
= if n < 1 then 1
Page 29 of 644
= Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation:
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Factorial
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
= if n < 1 then 1 JJ II
J I
= else n * fact (n-1);
Page 30 of 644
- Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation: Computing: The . . .
J I
val fact = fn : int -> int
Page 31 of 644
- fact 8; Go Back
- Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation:
Factorial Computing: The . . .
Algorithms: Design & . . .
J I
- fact 8;
Page 32 of 644
- Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation:
Factorial Computing: The . . .
Home Page
= else n * fact (n-1); Title Page
- fact 8; J I
- fact 9; Go Back
Full Screen
val it = 362880 : int
Close
- Quit
More SML
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
environments JJ II
• Is free! J I
Page 34 of 644
• http://www.smlnj.org Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Important
Features Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
Page 35 of 644
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
1.3. Primitives: Integer & Real
Computing: The . . .
1. Algorithms & Programs
Algorithms: Design & . . .
2. SML: Primitive Integer Operations 1 Introducing Reals
Correctness, . . .
3. SML: Primitive Integer Operations 1
Compound Data
4. SML: Primitive Integer Operations 1 Higher Order Functions . . .
Imperative . . .
5. SML: Primitive Integer Operations 1
A large Example: . . .
6. SML: Primitive Integer Operations 1 Lecture-wise Index to . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Quotient & Remainder
19. SML: Primitive Real Operations 1
20. SML: Primitive Real Operations 1
Computing: The . . .
JJ II
J I
Page 37 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithms & Programs Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
• Algorithm Correctness, . . .
Compound Data
Home Page
JJ II
• Programming
J I
• Standard ML
Go Back
Full Screen
Factorial
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
JJ II
Standard ML of New Jersey,
J I
- Page 39 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Operations 1
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
- val x = 5;
J I
val x = 5 : int Page 40 of 644
- Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Operations 1 Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
val x = 5 : int
J I
- val y = 6; Page 41 of 644
- Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer Computing: The . . .
JJ II
- val y = 6;
J I
val y = 6 : int Page 42 of 644
- x+y; Go Back
- Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
sml Correctness, . . .
Compound Data
JJ II
val y = 6 : int
J I
- x+y; Page 43 of 644
- Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 1 Computing: The . . .
Algorithms: Design & . . .
Standard ML of New Jersey, Introducing Reals
Correctness, . . .
- x+y; JJ II
val it = 11 : int J I
Full Screen
- it + 5; Close
- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val x = 5 : int Introducing Reals
Correctness, . . .
- x-y; JJ II
val it = ˜1 : int J I
- it + 5; Page 45 of 644
Full Screen
- x * y; Close
- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val y = 6 : int Introducing Reals
Correctness, . . .
- it + 5; JJ II
val it = 4 : int J I
- x * y; Page 46 of 644
Full Screen
- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val it = 11 : int Introducing Reals
Correctness, . . .
- x * y; JJ II
val it = 30 : int J I
Full Screen
- val b = 7; Close
- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
val it = ˜1 : int Introducing Reals
Correctness, . . .
- it + 5; Compound Data
Higher Order Functions . . .
- val a = 25; JJ II
val a = 25 : int J I
Full Screen
- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 2 Computing: The . . .
Algorithms: Design & . . .
- x * y; Introducing Reals
Correctness, . . .
val b = 7 : int JJ II
- val q = a div b; J I
Full Screen
- •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val a = 25; Introducing Reals
Correctness, . . .
- val b = 7; Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
val b = 7 : int Home Page
val q = 3 : int JJ II
- val r = a mod b; J I
Full Screen
- a = b*q + r; Close
- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val b = 7; Introducing Reals
Correctness, . . .
GC #0.0.0.0.2.45: (0 ms) JJ II
val r = 4 : int J I
Full Screen
- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val q = a div b; Introducing Reals
Correctness, . . .
- a = b*q + r; JJ II
Full Screen
- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
- val r = a mod b; Introducing Reals
Correctness, . . .
- val c = ˜7; JJ II
val c = ˜7 : int J I
Full Screen
- • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
SML: Primitive Integer
Operations 3 Computing: The . . .
Algorithms: Design & . . .
val r = 4 : int Introducing Reals
Correctness, . . .
- val q1 = a div c; JJ II
val q1 = ˜4 : int J I
Full Screen
- •
First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Quotient & Remainder Computing: The . . .
Algorithms: Design & . . .
Title Page
So J I
Page 55 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
J I
val real_a = 25.0 : real
Page 56 of 644
- Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Real
Operations 1 Computing: The . . .
Algorithms: Design & . . .
J I
operator domain: real * real
Page 57 of 644
real_a + b Close
- Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Real
Operations 1
Computing: The . . .
Home Page
- b + real_a; JJ II
Full Screen
in expression:
Close
b + real_a Quit
- • • •
First • • •
Prev • •
Next Last Go Back Full Screen Close Quit
SML: Primitive Real
Operations 2
Computing: The . . .
Title Page
val it = 3.57142857143 : real JJ II
- a div b; J I
Quit
- • •
First• • • •
Prev • •
Next Last Go Back Full Screen Close Quit
SML: Primitive Real Computing: The . . .
Algorithms: Design & . . .
Home Page
- trunc(c); Title Page
val it = 3 : int JJ II
- trunc (c + 0.5); J I
- Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Primitive Real
Operations 4
Computing: The . . .
Home Page
- - d+pi; Title Page
- d-pi; J I
- pi + d; Go Back
Close
- Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Precision
- pi + d*10.0; Computing: The . . .
Algorithms: Design & . . .
- d*100.0 -pi; JJ II
Page 62 of 644
- d*10.0 - pi;
Go Back
- Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci Numbers Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
F(0) = 1 Correctness, . . .
Compound Data
Higher Order Functions . . .
F(1) = 1 Imperative . . .
A large Example: . . .
Home Page
Title Page
if (n = 0) orelse (n = 1) J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Euclidean Algorithm
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
J I
Page 64 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2. Algorithms: Design & Refinement
Computing: The . . .
Algorithms: Design & . . .
2.1. Technical Completeness & Algorithms
Introducing Reals
1. Recapitulation: Integers & Real Correctness, . . .
Compound Data
2. Recap: Integer Operations
Higher Order Functions . . .
3. Recapitulation: Real Operations Imperative . . .
A large Example: . . .
4. Recapitulation: Simple Algorithms
Lecture-wise Index to . . .
5. More Algorithms
Home Page
6. Powering: Math
7. Powering: SML Title Page
8. Technical completeness JJ II
9. What SML says
J I
10. Technical completeness
Page 65 of 644
11. What SML says ... contd
12. Powering: Math 1 Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. Powering: Integer Version
17. Exceptions: A new primitive
18. Integer Power: SML
Computing: The . . .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Recapitulation: Correctness, . . .
Compound Data
Higher Order Functions . . .
Title Page
• Some algorithms J I
Page 67 of 644
Forward
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Integer Computing: The . . .
Algorithms: Design & . . .
J I
Full Screen
Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recapitulation: Real
Operations Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
JJ II
– Real addition & subtraction J I
Full Screen
Back Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recapitulation: Simple Computing: The . . .
Algorithms: Design & . . .
JJ II
– Factorial J I
Full Screen
Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
More Algorithms
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
• Combinations nCk J I
Page 71 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Powering: Math Computing: The . . .
Algorithms: Design & . . .
xn = x
A large Example: . . .
Home Page
n times
Title Page
tive definition: J I
Page 72 of 644
n 1 if n = 0
x = Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Powering: SML
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
if n = 0 Home Page
J I
Page 73 of 644
Is it technically complete? Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Technical Computing: The . . .
Algorithms: Design & . . .
completeness
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
JJ II
• n will be non-negative? J I
• x 6= 0? Page 74 of 644
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What SML says
sml Computing: The . . .
Algorithms: Design & . . .
Standard ML of New Jersey Introducing Reals
Correctness, . . .
Home Page
val it = () : unit JJ II
J I
Page 75 of 644
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Technical Computing: The . . .
Algorithms: Design & . . .
• x 6= 0? NO Title Page
JJ II
If x = 0 what is 0.00? J I
Page 76 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What SML says ...
contd Computing: The . . .
Algorithms: Design & . . .
J I
- power (0.0, 3);
Page 77 of 644
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
1.0/x−n if n < 0
Home Page
Title Page
xn = 1 if n = 0 JJ II
n−1
x × x otherwise J I
Page 78 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Powering: SML 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
fun power (x, n) = Compound Data
Higher Order Functions . . .
if n < 0 Imperative . . .
A large Example: . . .
Home Page
then 1.0 JJ II
Page 79 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Technical Completness Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
itive n Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
0.0n JJ II
= 1.0/(0.0m) J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What SML says
- power (2.5, ˜2); Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
val it = 0.16 : real Correctness, . . .
Compound Data
JJ II
- power (0.0, ˜2);
J I
val it = inf : real Page 81 of 644
- Go Back
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Powering: Integer Computing: The . . .
Version
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
xn = 1 if n = 0 Lecture-wise Index to . . .
J I
to consider the case n < 0. Other-
Page 82 of 644
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Exceptions: A new Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
primitive Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
then raise negExponent
J I
else if n = 0 then 1 Page 83 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Integer Power: SML
Computing: The . . .
Home Page
JJ II
- Go Back
Full Screen
Close
Back to More Algos
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Integer Square Root 1
√ Computing: The . . .
JJ II
- isqrt (38);
J I
val it = 6 : int Page 85 of 644
- Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Integer Square Root 2 Computing: The . . .
(k + 1)2
Home Page
Title Page
That is, JJ II
⊥ if n < 0 J I
isqrt(n) =
k otherwise Page 86 of 644
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An analysis Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
0 ≤ k 2 ≤ n < (k + 1)2
√
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
⇒ 0≤k ≤ n<k+1 Lecture-wise Index to . . .
Title Page
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
Page 88 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
shrink(n, 0, n) if n > 0
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithm: shrink Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
l if l = u Lecture-wise Index to . . .
shrink(n, l + 1, u) if l < u Home Page
and (l + 1)2 ≤ n
Title Page
JJ II
shrink(n, l, u − 1) if l < u J I
and u 2>n Page 90 of 644
⊥ if l > u
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: shrink
Computing: The . . .
Title Page
then raise intervalError JJ II
else if (l+1)*(l+1) <= n J I
else l; Go Back
Full Screen
Close
intsqrt
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: intsqrt Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
else if n=0 JJ II
then 0 J I
Go Back
Full Screen
shrink Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run it!
exception intervalError Computing: The . . .
Algorithms: Design & . . .
val it = 2 : int JJ II
- intsqrt 16; J I
Go Back
- intsqrt 99;
Full Screen
- Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
• Is shrink general-purpose JJ II
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intsqrt: Reorganized Computing: The . . .
Algorithms: Design & . . .
else if n=0 JJ II
J I
then 0
Page 95 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
shrink: Another
algorithm Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Shrink Correctness, . . .
Compound Data
shrink2(n, m, u) if l < u Title Page
and m2 ≤ n
JJ II
J I
shrink2(n, l, m) if l < u Page 96 of 644
and m 2>n
Go Back
⊥ if l > u
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
else if l = u J I
Page 97 of 644
then l
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shrink2: SML ... contd Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
else Correctness, . . .
Compound Data
Higher Order Functions . . .
let val m = (l+u) div 2; Imperative . . .
A large Example: . . .
Title Page
then shrink (n, m, u)
JJ II
else shrink (n, l, m) J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2.2. Algorithm Refinement
Computing: The . . .
1. Recap: More Algorithms
Algorithms: Design & . . .
2. Recap: Power Introducing Reals
Correctness, . . .
3. Recap: Technical completeness
Compound Data
4. Recap: More Algorithms Higher Order Functions . . .
Imperative . . .
5. Intsqrt: Reorganized
A large Example: . . .
6. Intsqrt: Reorganized Lecture-wise Index to . . .
8. Combinations: Math
Title Page
9. Combinations: Details
JJ II
10. Combinations: SML
11. Perfect Numbers J I
12. Refinement
Page 99 of 644
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. SML: Assembly 2
19. Perfect Numbers . . . contd.
20. Perfect Numbers . . . contd.
Computing: The . . .
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Recap: More
Compound Data
Higher Order Functions . . .
Imperative . . .
Algorithms
A large Example: . . .
Lecture-wise Index to . . .
Home Page
JJ II
• Integer square root J I
Forward
Page 101 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Power Computing: The . . .
Title Page
– More complete definition for real JJ II
x J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Technical
completeness Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
• n will be non-negative? NO JJ II
• x 6= 0? NO J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: More Computing: The . . .
Algorithms: Design & . . .
JJ II
– Algorithmic idea
J I
– where Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intsqrt: Reorganized
exception negError; Computing: The . . .
Algorithms: Design & . . .
u*u < n JJ II
J I
then raise intervalError
Page 105 of 644
else l; Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Intsqrt: Reorganized Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
in if n<0 Imperative . . .
A large Example: . . .
Title Page
then 0 JJ II
else shrink (n, 0, n) J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Home Page
• Perfect Numbers JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations: Math
nC = n! Computing: The . . .
k (n−k)!k! Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
= k!
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
n(n−1)···(k+1)
= (n−k)! Title Page
JJ II
= n−1Ck−1 + n−1Ck
J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations: Details
Computing: The . . .
Home Page
k < 0 or
Title Page
k>n
JJ II
nC = 1 if n = 0 or J I
k
k = 0 or
Page 109 of 644
k=n Go Back
n−1
Ck−1 + n−1Ck otherwise
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations: SML
exception invalid_arg;
fun comb (n, k) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
if n < 0 orelse Correctness, . . .
Compound Data
JJ II
k = 0 orelse
J I
n = k Page 110 of 644
then 1 Go Back
Quit
Home Page
Title Page
perf ect(n) JJ II
J I
P
⇐⇒ n = {k : 0 < k < n, k|n} Page 111 of 644
Go Back
Pn−1
⇐⇒ n = k=1 if divisor(k)
Full Screen
Close
where Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Refinement Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
fined algorithmically. J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers: SML Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
else JJ II
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Pu
l if divisor(k) Computing: The . . .
Algorithms: Design & . . .
Pu Introducing Reals
k=l if divisor(k) =
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
if l > u
Lecture-wise Index to . . .
0 Home Page
Title Page
if divisor(l)+ otherwise JJ II
n−1 if divisor(k)
P
J I
k=l+1
Page 114 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: sum divisors
Computing: The . . .
From
Pu the algorithmic definition of Algorithms: Design & . . .
Introducing Reals
k=l if divisor(k)
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
then 0 JJ II
Go Back
Full Screen
defined Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
if divisor and ifdivisor
Computing: The . . .
Algorithms: Design & . . .
if divisor(k) = Correctness, . . .
JJ II
then k J I
else 0 Page 116 of 644
Go Back
However . . . Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Assembly 1
fun sum_divisors (l, u) = Computing: The . . .
Algorithms: Design & . . .
else 0 JJ II
J I
in ifdivisor (l) +
Page 117 of 644
sum_divisors (l+1, u) Go Back
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Assembly 2
exception nonpositive; Computing: The . . .
Home Page
... JJ II
Go Back
Full Screen
n > 0.
Technically complete! Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers
. . . contd.
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
Hence JJ II
J I
n−1
X n div
X 2 Page 119 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers
. . . contd. Computing: The . . .
Algorithms: Design & . . .
Title Page
Pn div 2 JJ II
⇐⇒ n = k−1 if divisor(k) J I
where
Page 120 of 644
Go Back
k if k|n Full Screen
if divisor(k) =
0 otherwise Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Assembly 3
exception nonpositive;
fun perfect (n) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
JJ II
...
J I
in n = sum_divisors (1, n div 2) Page 121 of 644
end Go Back
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers: Run Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect Numbers: Run Computing: The . . .
Algorithms: Design & . . .
GC #0.0.0.1.3.88: (1 ms) JJ II
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Code variations
exception nonpositive;
fun perfect (n) = Computing: The . . .
Algorithms: Design & . . .
in J I
Close
in if n <= 0 JJ II
end Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
SML: Code variations
Computing: The . . .
Title Page
if n <= 0
JJ II
then raise nonpositive J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Summation:
Generalizations
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
0 if l > u
u
J I
X
f (i) = Page 127 of 644
i=l u
i=l+1 f (i) Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Algorithmic Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
1. perf ect2 JJ II
2. shrink2 J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithmic Variations Computing: The . . .
√
3. For n > 2, b nc < n div 2 Title Page
Pn div 2 JJ II
b nc
X Go Back
k=2 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Algorithmic Variations Computing: The . . .
Algorithms: Design & . . .
Pb√nc Imperative . . .
A large Example: . . .
Home Page
0 otherwise
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2.3. Variations: Algorithms & Code
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Recap Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
• Code Variations JJ II
• Algorithmic Variations J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Combinations Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
nC = n! Compound Data
k (n−k)!k! Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
n(n−1)···(n−k+1)
= k!
Home Page
Title Page
JJ II
n(n−1)···(k+1)
= (n−k)! J I
= n−1Ck−1 + n−1Ck
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations 1 Computing: The . . .
Algorithms: Design & . . .
k > n JJ II
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations 2
exception invalid_arg;
fun comb (n, k) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
if n < 0 orelse Correctness, . . .
Compound Data
k = 0 orelse JJ II
J I
n = k Page 135 of 644
then 1 Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Combinations 3
exception invalid_arg;
fun comb (n, k) = Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
if n < 0 orelse Correctness, . . .
Compound Data
k = 0 orelse JJ II
J I
n = k Page 136 of 644
then 1 Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Perfect 2
Computing: The . . .
Algorithms: Design & . . .
perf ect(n) Introducing Reals
Correctness, . . .
Compound Data
Title Page
if divisor2(k) = JJ II
k + m if k|n and k = 6 m J I
Go Back
0 otherwise
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
xn = |(x × x ×
{z· · · × x)} ×x Title Page
JJ II
n−1 times
J I
ently Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Faster Power
xn = |(x × x ×{z· · · × x)} Computing: The . . .
Algorithms: Design & . . .
Title Page
bn/2c times J I
Go Back
× x Close
Quit
when n is odd •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
1.0/power2(x, n) if n < 0 Home Page
1.0 if n = 0 Title Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Title Page
else if n = 0 JJ II
then 1.0 J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Power2: SML
let fun even m = Computing: The . . .
Algorithms: Design & . . .
square (pwr_n_by_2) JJ II
in if even (n) J I
end Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Computation: Issues
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
J I
(c) Termination Page 143 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
General Correctness Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Code: Justification Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
ness of Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
J I
Full Screen
incorrectness
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recall
• A program is an Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
– explicit, Correctness, . . .
Compound Data
Higher Order Functions . . .
– technically complete
Lecture-wise Index to . . .
Home Page
mathematical notation. JJ II
J I
• Moreover, mathematical notation is Page 146 of 644
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Features: Definition Computing: The . . .
Title Page
defining it. J I
Go Back
used. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run ifdivisor Computing: The . . .
Algorithms: Design & . . .
; JJ II
stdIn:18.8 Error: J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Diagnosis: Features of Computing: The . . .
Algorithms: Design & . . .
Home Page
perf ect(n). J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Back to Math
incorrect version
Computing: The . . .
0 otherwise Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
0 if l > u JJ II
if divisor(l)+ otherwise J I
sum divisors(l + 1, u)
Page 150 of 644
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Incorrectness
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
incorrect version Compound Data
Higher Order Functions . . .
k Home Page
JJ II
• But that is not made explicit in its J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ifdivisor3
Let Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
k if k|n Correctness, . . .
Home Page
0 if l > u Title Page
sum divisors(l + 1, u) J I
Page 152 of 644
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run it! Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
= else 0; JJ II
val ifdivisor3 = J I
- Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Try it!
Computing: The . . .
Algorithms: Design & . . .
- fun sum_divisors (l, u) = Introducing Reals
Correctness, . . .
= then 0 Imperative . . .
A large Example: . . .
Home Page
variable or constructor: n J I
Go Back
n! Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The n’s
Computing: The . . .
0 otherwise Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
0 if l > u JJ II
if divisor3(n, l)+ otherwise J I
sum divisors(l + 1, u)
Page 156 of 644
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
tions J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
scopes J I
Go Back
forward
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2.4. Names, Scopes & Recursion
Computing: The . . .
1. Disjoint Scopes
Algorithms: Design & . . .
2. Nested Scopes Introducing Reals
Correctness, . . .
3. Overlapping Scopes
Compound Data
4. Spannning Higher Order Functions . . .
Imperative . . .
5. Scope & Names
A large Example: . . .
6. Names & References Lecture-wise Index to . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. local...in...end
19. local...in...end
20. local...in...end
Computing: The . . .
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Disjoint Scopes
let Computing: The . . .
let
Compound Data
Higher Order Functions . . .
... Imperative . . .
A large Example: . . .
in Lecture-wise Index to . . .
fun fun2 z =
JJ II
let J I
... Page 161 of 644
in
in ... Go Back
end Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Nested Scopes
let Computing: The . . .
Title Page
in JJ II
x + y J I
in Go Back
Full Screen
fun1 x Close
end Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Overlapping Scopes
let Computing: The . . .
... Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
JJ II
J I
... Page 163 of 644
in ... Go Back
Full Screen
end Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Spannning
let Computing: The . . .
... Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
JJ II
fun fun2 z =
J I
... Page 164 of 644
in ... Go Back
Full Screen
end Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scope & Names Computing: The . . .
Algorithms: Design & . . .
Title Page
Full Screen
diagram
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 166 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 167 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 168 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 169 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 170 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 171 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val z = 5; Computing: The . . .
Algorithms: Design & . . .
let
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
in Title Page
x + y * z JJ II
end J I
in
Page 172 of 644
Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val x = x − 5; Computing: The . . .
Algorithms: Design & . . .
let Correctness, . . .
in Imperative . . .
A large Example: . . .
Title Page
fun fun2 z =
let
JJ II
... J I
in
... Page 173 of 644
end Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val x = x − 5; Computing: The . . .
Algorithms: Design & . . .
let Correctness, . . .
in Imperative . . .
A large Example: . . .
Title Page
fun fun2 z =
let
JJ II
... J I
in
... Page 174 of 644
end Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Names & References
let
val x = 10; val x = x − 5; Computing: The . . .
Algorithms: Design & . . .
let Correctness, . . .
in Imperative . . .
A large Example: . . .
Title Page
fun fun2 z =
let
JJ II
... J I
in
... Page 175 of 644
end Go Back
end Close
Quit
Back
•Firstto scope
•Prev •Next •names
Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Use of Names Computing: The . . .
Algorithms: Design & . . .
• by themselves – to be evaluated
A large Example: . . .
Lecture-wise Index to . . .
Home Page
in expression Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect Computing: The . . .
Algorithms: Design & . . .
k <= 0 orelse JJ II
n < k J I
Go Back
else if n mod k = 0 Full Screen
then k Close
else 0; Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect
Computing: The . . .
Title Page
u > n JJ II
then raise invalidArg J I
then 0 Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect Computing: The . . .
Algorithms: Design & . . .
in Introducing Reals
Correctness, . . .
let JJ II
Go Back
end Close
end Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
local...in...end
perfect
Computing: The . . .
Standard ML of New Jersey, Algorithms: Design & . . .
Introducing Reals
JJ II
- perfect 28;
J I
val it = true : bool Page 181 of 644
- perfect 6; Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scope & local
local Computing: The . . .
JJ II
J I
end Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computations: Simple Computing: The . . .
Algorithms: Design & . . .
Home Page
• presence of brackets JJ II
J I
Full Screen
Hence Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Simple computations
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
4 + 6 − (4 + 6) div 2
A large Example: . . .
Lecture-wise Index to . . .
JJ II
= 10 − 5 J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computations: Composition
Computing: The . . .
Home Page
= f (3 ∗ 4 + 2) JJ II
= f (14) J I
= 196 + 1 Go Back
Full Screen
= 197 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Composition:
Alternative
Computing: The . . .
f (x) = x2 + 1
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
= g(4)2 + 1 JJ II
= (3 ∗ 4 + 2)2 + 1 J I
(12 + 2)2 + 1
Page 186 of 644
= Go Back
= 196 + 1 Close
= 197 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Compositions: Computing: The . . .
Compare
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
f (g(a)) f (g(a)) A large Example: . . .
= f (3 ∗ 4 + 2) = g(4)2 + 1
Lecture-wise Index to . . .
Home Page
= = (12 + 2)2 + 1 JJ II
= 142 + 1 = 142 + 1 J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Compositions: Correctness, . . .
Compound Data
Higher Order Functions . . .
Compare Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
Question 2: Which is easier to imple- J I
ment?
Question 3: Which is more efficient? Page 188 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
J I
b
Page 189 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
1 if n = 0 Lecture-wise Index to . . .
f actL(n) =
f actL(n − 1) ∗ n otherwise
Home Page
Title Page
1 if n = 0 JJ II
f actR(n) =
n ∗ f actR(n − 1) otherwise J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Left
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
= ((f actL(2) ∗ 3) ∗ 4) JJ II
J I
= (((f actL(2 − 1) ∗ 2) ∗ 3) ∗ 4) Page 191 of 644
= ··· Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Right Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
(4 ∗ f actR(4 − 1))
A large Example: . . .
= Lecture-wise Index to . . .
JJ II
= (4 ∗ (3 ∗ f actR(2))) J I
= ··· Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
3. Introducing Reals
Computing: The . . .
Algorithms: Design & . . .
3.1. Floating Point
Introducing Reals
1. So Far-1: Computing Correctness, . . .
Compound Data
2. So Far-2: Algorithms & Programs
Higher Order Functions . . .
3. So far-3: Top-down Design Imperative . . .
A large Example: . . .
4. So Far-4: Algorithms to Programs
Lecture-wise Index to . . .
5. So far-5: Caveats
Home Page
6. So Far-6: Algorithmic Variations
7. So Far-7: Computations Title Page
8. Floating Point JJ II
9. Real Operations
J I
10. Real Arithmetic
Page 193 of 644
11. Numerical Methods
12. Errors Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. Equation Solving
17. Root Finding-1
18. Root Finding-2
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
J I
• The digital computer & program- Page 195 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-2: Algorithms & Computing: The . . .
Algorithms: Design & . . .
Home Page
explications of algorithms JJ II
J I
• Standard ML: Its primitives Page 196 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So far-3: Top-down
Design Computing: The . . .
design Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
plete definition of the function
J I
Close
Perfect
•First Numbers
•Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-4: Algorithms to Computing: The . . .
Algorithms: Design & . . .
Programs
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
a Program JJ II
J I
eralize Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So far-5: Caveats Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
– unless properly justified J I
ness Go Back
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-6: Algorithmic Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Variations Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
ciples. Thus ensuring their JJ II
– mathematical correctness J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
So Far-7:
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Computations Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
– Composition of functions J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Floating Point
• Each real number 3E11 is repre- Computing: The . . .
Algorithms: Design & . . .
Title Page
by
JJ II
tation. Go Back
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Operations Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
volved Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Arithmetic
• for addition and subtraction the two Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
precision JJ II
Full Screen
back Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Numerical Methods
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Title Page
absolute error = a∗ − a J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Errors Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
pute the value of a (convergent) in-
J I
finite series because computations Page 207 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Infinite Series
cannot be computed to ∞ Computing: The . . .
Algorithms: Design & . . .
∞ m Introducing Reals
X x
ex
Correctness, . . .
= Compound Data
m=0 Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
∞
(−1)mx2m
Home Page
X
cos x = Title Page
(2m)! JJ II
m=0
J I
X (−1)mx2m+1 Go Back
sin x =
(2m + 1)! Full Screen
m=0 Close
Quit
Truncation
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Truncation Errors
and hopefully it is good enough to re-
strict it to appropriate values of n Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
n
xm
Correctness, . . .
X
ex
Compound Data
m! Imperative . . .
A large Example: . . .
m=0 Lecture-wise Index to . . .
Home Page
n
X (−1)mx2m Title Page
cos x = JJ II
(2m)!
m=0 J I
n
X (−1)mx2m+1 Go Back
(2m + 1)!
m=0 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
equation solving JJ II
J I
f (x) = 0 Page 210 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-1
Computing: The . . .
f(b)
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
x0 Home Page
a Title Page
b JJ II
J I
f(a) Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-2
Computing: The . . .
f(b)
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
x0 Home Page
a Title Page
b JJ II
J I
f(a) Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-3
Computing: The . . .
f(b)
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
x0 Home Page
a Title Page
b JJ II
J I
f(a) Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding-4
Rather steep isn’t it?
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
f(b) Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
x0
Home Page
Title Page
a
JJ II
b J I
Go Back
Close
ε Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
3.2. Root Finding, Composition and Recursion
Computing: The . . .
1. Root Finding: Newton’s Method
Algorithms: Design & . . .
2. Root Finding: Newton’s Method Introducing Reals
Correctness, . . .
3. Root Finding: Newton’s Method
Compound Data
4. Root Finding: Newton’s Method Higher Order Functions . . .
Imperative . . .
5. Root Finding: Newton’s Method
A large Example: . . .
6. Root Finding: Newton’s Method Lecture-wise Index to . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. What’s it good for? 2
19. newton: Computation
20. Generalized Composition
Computing: The . . .
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s Computing: The . . .
Algorithms: Design & . . .
J I
erywhere in [a, b] Page 217 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
a xi
Page 219 of 644
b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
xi+1 xi
a αi Page 220 of 644
b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
xi+1 xi
a αi Page 221 of 644
b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Newton’s
Method Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
xi+1 xi
a αi Page 222 of 644
xi+2 b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton’s Method:
Basis Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
f (x ) Correctness, . . .
f (x )
xi+1 = xi − f 0(xi ) Home Page
i Title Page
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton’s Method:
Basis Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
JJ II
|f (xn)| < ε J I
Go Back
x 0 , x1 , x2 , · · · Full Screen
converges to a root of f .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton’ Method:
Algorithm
Computing: The . . .
Then Correctness, . . .
Compound Data
newton(f, f 0, a, b, ε, xi) =
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
where J I
x0 ∈ [a, b] Page 225 of 644
and Go Back
Full Screen
f (xi)
xi+1 = xi − 0 ∈ [a, b] Close
f (xi) Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-1
Computing: The . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
xi+1 xi
a αi Page 226 of 644
xi+2 b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
[a, b]! The function may not satisfy all Home Page
Title Page
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-2
Computing: The . . .
f(b)
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
xi+1 xi
a αi Page 228 of 644
b Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-3
The function may be too steep Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
f(b)
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
x0 Home Page
a Title Page
b JJ II
J I
Full Screen
ε
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What can go wrong!-4
Computing: The . . .
Home Page
x0
f(b) Title Page
a
JJ II
b
f(a) J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Computations &
Induction: 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
x 0 , x 1 , x 2 , . . . xn Lecture-wise Index to . . .
Home Page
JJ II
that even if the sequence is not totally
J I
ordered, there is some discrete con- Page 231 of 644
|f (xi) − 0|
Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Real Computations & Computing: The . . .
Home Page
JJ II
we have J I
Go Back
multiples of ε Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What’s it good for? 1
√
n
Finding the positive n-th root c of a Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
c > 0 and n > 1 amounts to solving Correctness, . . .
Compound Data
xn = c
A large Example: . . .
Lecture-wise Index to . . .
Home Page
of f (x) with JJ II
f (x) = xn − c
J I
f 0(x) = nxn−1
Page 233 of 644
Go Back
√
with [a, b] = [0, c] or [0, c] and an ap- Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
What’s it good for? 2
Finding roots of polynomials. Computing: The . . .
Algorithms: Design & . . .
n Introducing Reals
ci xi
X Correctness, . . .
n Home Page
icixi−1
X
f 0(x) = Title Page
JJ II
i=1 J I
Go Back
• an appropriately chosen ε, Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
newton : Computation Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
newton(f, f 0, a, b, ε, x1)
A large Example: . . .
Lecture-wise Index to . . .
.. .. .. JJ II
J I
xn Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Generalized
Composition
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
where
Home Page
f (x, y) = JJ II
y otherwise J I
0 if x = 0 Go Back
g(x) =
g(x − 1) + 1 otherwise Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
g(0) + 1 | f (1, (0 + 1) J I
1 | 1 Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
| FOREVER! Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursive Computing: The . . .
Algorithms: Design & . . .
Computations
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
• Newton’s method Home Page
JJ II
– f actL J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Left
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
((f actL(2) ∗ 3) ∗ 4) JJ II
J I
(((f actL(2 − 1) ∗ 2) ∗ 3) ∗ 4) Page 240 of 644
··· Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Right Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
(4 ∗ f actR(4 − 1))
A large Example: . . .
Lecture-wise Index to . . .
JJ II
(4 ∗ (3 ∗ f actR(2))) J I
··· Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Nonlinear Computing: The . . .
Algorithms: Design & . . .
Title Page
··· Go Back
Full Screen
contd ...
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Some Practical
Questions
• What is the essential difference be- Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
Full Screen
Answer • • •
First• •Prev Next Last Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
f actR? Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4. Correctness, Termination & Complexity
4.1. Termination and Space Complexity
1. Recursion Revisited Computing: The . . .
Algorithms: Design & . . .
2. Linear Recursion: Waxing
Introducing Reals
7. Unfolding Recursion
Home Page
8. Non-termination
Title Page
9. Termination
10. Proofs of termination JJ II
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Induction is Well-founded
19. Induction is Well-founded
20. Where it doesn’t work
Computing: The . . .
21. Well-foundedness is inductive Algorithms: Design & . . .
Introducing Reals
22. Well-foundedness is inductive
Correctness, . . .
23. GCD: Well-foundedness Compound Data
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Recursion Revisited
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
– Waning JJ II
J I
• Non-linear recursion Page 247 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Waxing Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
((f actL(2) ∗ 3) ∗ 4) JJ II
(((f actL(1) ∗ 2) ∗ 3) ∗ 4) J I
Go Back
contrast with newton
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
(6 ∗ 4) JJ II
J I
24 Page 249 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Nonlinear Recursions
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Fibonacci A large Example: . . .
Lecture-wise Index to . . .
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
··· J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion: Waxing &
Waning Computing: The . . .
Home Page
J I
– name by value replacements Page 252 of 644
Quit
– Unfolding recursion • • • • •
First Prev Next Last Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
leading to nontermination J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Non-termination Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Algorithm Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
leads to nontermination JJ II
J I
• Recursion may lead to non- Page 254 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
components. Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Proofs of termination
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
• if it is inductive, or JJ II
J I
• it is well-founded Page 256 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proofs of termination:
Induction
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
– Factorial J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof of termination: Computing: The . . .
JJ II
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof of termination: Computing: The . . .
Algorithms: Design & . . .
Title Page
f actL(n) ∗ (n + 1) is guaranteed to J I
Full Screen
back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Termination
Fibonacci
Computing: The . . .
J I
f ib(n) and f ib(n − 1) is guaranteed Page 260 of 644
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
gcd(12, 4) JJ II
gcd(4, 0) J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Well-foundedness:
GCD Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
is Title Page
and J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Well-foundedness Computing: The . . .
Home Page
J I
2. with each successive recursive call Page 263 of 644
least 1. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Induction is Computing: The . . .
Algorithms: Design & . . .
Home Page
to terminate JJ II
• Factorial w(n) ∝ n J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Well-founded
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
decreasing. Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Where it doesn’t work Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
Go Back
founded. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
J I
• it serves no useful purpose for the Page 267 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Well-foundedness is
inductive Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
J I
in 1-1 correspondence with the set Page 268 of 644
ind(w(~x)) = n − i
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
GCD:
Correctness, . . .
Compound Data
Higher Order Functions . . .
Well-foundedness Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
w(a, b) = b J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton:
Well-foundedness Computing: The . . .
Algorithms: Design & . . .
Home Page
JJ II
x 0 , x 1 , x 2 , . . . xn J I
Close
where Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton: Computing: The . . .
Algorithms: Design & . . .
Title Page
w(x) = b c Go Back
ε Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Example: Zero Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
tion Imperative . . .
A large Example: . . .
Home Page
zero(x + 1.0) if x ≤ −1.0 Title Page
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
Go Back
The Collatz Problem
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Collatz Problem Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
nate? Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
collatz(m) = Home Page
1 if m ≤ 1 Title Page
J I
collatz(3 ∗ m + 1) otherwise
Page 274 of 644
m. Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Questions
Q: what other uses can well-founded Computing: The . . .
Algorithms: Design & . . .
Title Page
Space Complexity : The amount of
JJ II
memory space required, as a func- J I
Full Screen
(number of computation steps) as a Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
• Euclidean GCD JJ II
• Factorial J I
• Fibonacci Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Newton & Euclid:
Absolute
Computing: The . . .
Newton’s Method Algorithms: Design & . . .
JJ II
Go Back
Go Back
Euclidean GCD
Computation Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Deriving space
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
requirements
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
GCD: Space Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
1 if b = 0 Correctness, . . .
Compound Data
Sgcd(a,b) = Higher Order Functions . . .
Sgcd(b,a mod b) otherwise Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
J I
Sgcd(a,b) ∝ 1 Page 280 of 644
Go Back
newton Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Space
f actL
Computing: The . . .
1 if n = 0 Introducing Reals
Sf actL(n) = Correctness, . . .
Title Page
n”. J I
Sf actL(n) ∝ n. Go Back
Full Screen
f actR. Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Sf ib(n) =
Sf ib(n−1) + Sf ib(n−2) if n > 1 JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Space
Fibonacci
JJ II
mately doubles. Further, it is easy to
J I
show by induction that Page 283 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4.2. Efficiency Measures and Speed-ups
Computing: The . . .
1. Recapitulation
Algorithms: Design & . . .
2. Recapitulation Introducing Reals
Correctness, . . .
3. Time & Space Complexity
Compound Data
4. isqrt: Space Higher Order Functions . . .
Imperative . . .
5. Time Complexity
A large Example: . . .
6. isqrt: Time Complexity Lecture-wise Index to . . .
12. Comparisons
Page 284 of 644
13. Comparisons
Go Back
14. Efficiency Measures: Time
15. Efficiency Measures: Space Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Factoring out calculations
19. Tail Recursion: 1
20. Tail Recursion: 2
Computing: The . . .
29. Invariance JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
• Well-foundedness proofs JJ II
J I
• Well-foundedness & Complexity Page 286 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recapitulation Computing: The . . .
Title Page
– well-founded functions JJ II
– By well-founded functions J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Time & Space
Complexity Computing: The . . .
Algorithms: Design & . . .
Questions Introducing Reals
Correctness, . . .
Compound Data
JJ II
• Newton & Euclid’s GCD J I
Go Back
– Factorial Close
– Fibonacci Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
isqrt : Space
Computing: The . . .
Integer Sqrt shrink Algorithms: Design & . . .
Introducing Reals
1
if l = u Home Page
JJ II
shrink(n,l,u−1) if l < u . . .
S
J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
• Factorial J I
Go Back
forward
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
isqrt: Time Complexity
Integer Sqrt Computing: The . . .
Title Page
0
if l = u JJ II
1 + Tshrink(n,l+1,u) if l < u . . . J I
shrink(n,l,u−1) if l < u . . .
1+T
Page 291 of 644
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
isqrt2: Time
shrink
Imperative . . .
if u ≤ l ≤ u
A large Example: . . .
0
Lecture-wise Index to . . .
1+T
if m 2>n Title Page
shrink2(n,l,u−1) JJ II
J I
If 2k−1 ≤ |[l, u]| − 1 < 2k then the al- Page 292 of 644
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
shrink vs. shrink2 : Times
shrink shrink2
Computing: The . . .
Algorithms: Design & . . .
JJ II
Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Time Computing: The . . .
Title Page
0 if n = 0
Tf actL(n) = JJ II
Tf actL(n−1)+1 otherwise J I
Then Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Time
Complexity Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Fibonacci Compound Data
Higher Order Functions . . .
Tf ib(n) =
Tf ib(n−1) + Tf ib(n−2) if n > 1 J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
O(2n) O(2n)
Page 296 of 644
f ib(n) Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Comparisons Computing: The . . .
Algorithms: Design & . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Comparisons Computing: The . . .
Algorithms: Design & . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Efficiency Measures: Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Time Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Space Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Speeding Up: 1
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
J I
and attempting more efficient algo- Page 301 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
J I
shrink Page 302 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factoring out Computing: The . . .
JJ II
gcd(a2, b2) J I
an Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Tail Recursion: 1
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
J I
and include them as arguments of
Page 304 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
operation. Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Tail
Recursion
Computing: The . . .
f actL Waxing f actL Waning Algorithms: Design & . . .
Introducing Reals
Home Page
terms of p. Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Tail
Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Factorial Correctness, . . .
Compound Data
⊥ if n < 0 Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
f actL2(n) = 1 if n = 0 Home Page
where JJ II
J I
f actL tr(n, p) = Page 307 of 644
p if n = 0 Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Computation Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Factorial: Issues
• Correctness: Prove (by induction
Computing: The . . .
n!. Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
Quit
Complexity table
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Tail Computing: The . . .
Title Page
bonacci numbers f ib(m − 2) and JJ II
f ib(m − 1) required for the compu- J I
Full Screen
n, a, b, m. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fibonacci: Tail
Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
1 if 0 ≤ n ≤ 1 Home Page
f ib iter(n, 1, 1, 1) otherwise
Title Page
JJ II
where J I
f ib iter(n, a, b, m) = Page 311 of 644
b if m ≥ n Go Back
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
fibTR: SML
Computing: The . . .
Title Page
in
JJ II
fun fibTR (n) = J I
end; Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariance Computing: The . . .
Algorithms: Design & . . .
Title Page
cessive invocation JJ II
Go Back
algorithm and is crucial to the proof
Full Screen
of correctness Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4.3. Invariance & Correctness
Computing: The . . .
1. Recap
Algorithms: Design & . . .
2. Recursion Transformation Introducing Reals
Correctness, . . .
3. Tail Recursion: Examples
Compound Data
4. Comparisons Higher Order Functions . . .
Imperative . . .
5. Transformation Issues
A large Example: . . .
6. Correctness Issues 1 Lecture-wise Index to . . .
8. Correctness Theorem
Title Page
9. Invariants & Correctness 1
JJ II
10. Invariants & Correctness 2
11. Invariance Lemma: f actL tr J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Variants & Invariants
19. Variants & Invariants
20. More Invariants
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
• Comparative Complexity
Title Page
JJ II
• Comparisons: J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursion Computing: The . . .
Title Page
Go Back
ation Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Tail Recursion:
Correctness, . . .
Compound Data
Higher Order Functions . . .
Examples Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Comparisons
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
JJ II
f actL2(n) O(1) O(n) J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
n that every computation is finite. J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Correctness Issues 2 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
• Absolute correctness: Correctness, . . .
Compound Data
JJ II
Full Screen
rect. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariants &
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness 1 Correctness, . . .
Compound Data
Higher Order Functions . . .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariants & Computing: The . . .
JJ II
to the function that uses it. J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Invariance Lemma:
Correctness, . . .
Compound Data
Higher Order Functions . . .
f actL tr Imperative . . .
A large Example: . . .
Home Page
Proof: By induction on n. J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariance: Example Computing: The . . .
Algorithms: Design & . . .
Title Page
Go Back
tion Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Invariance: Example Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
f actL2 Correctness, . . .
Compound Data
Go Back
168
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof
Basis For n = 0, f actL tr(0, p) = p = Computing: The . . .
Algorithms: Design & . . .
JJ II
f actL tr(n + 1, p) J I
Full Screen
= (n + 1)!p Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Invariance Lemma:
f ib iter
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
f ib iter F Imperative . . .
A large Example: . . .
Home Page
then JJ II
INV :f ib iter(n, a, b, m) = F(n) J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Proof
Basis For k = 0, n = m, it follows that Computing: The . . .
Go Back
= f ib iter(n, b, a + b, m + 1) Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
1 if n = 0 JJ II
where Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
p if n = m Title Page
J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Invariants Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
√ Home Page
l ≤ b nc ≤ u Title Page
• shrink2 JJ II
√ Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
1.0/power3(x, −n) if n < 0 Title Page
1.0 if n = 0 JJ II
where Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Fast Powering 2
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
power2 Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
powerT R(x2, n div 2, xp) otherwise
J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding: Bisection Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Newton’s Method Algorithm Correctness, . . .
Compound Data
Higher Order Functions . . .
Select a small enough ε > 0 and Imperative . . .
A large Example: . . .
Title Page
c if |f (c)| < ε JJ II
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Advantage Bisection
Computing: The . . .
More robust than Newton’s method Algorithms: Design & . . .
Introducing Reals
sign Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
J I
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5. Compound Data
5.1. Tuples, Lists & the Generation of Primes
1. Recap: Tail Recursion Computing: The . . .
Algorithms: Design & . . .
2. Examples: Invariants
Introducing Reals
3. Tuples Correctness, . . .
Compound Data
4. Lists Higher Order Functions . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Density of Primes
19. The Prime Number Theorem
20. The Prime Number Theorem
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recap: Tail Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
through J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Examples: Invariants Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
√
l ≤ b nc ≤ u
Home Page
√ Title Page
J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Tuples
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists Computing: The . . .
Algorithms: Design & . . .
Title Page
Go Back
cessible. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
New Lists
Given a (nonempty) list L, Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
Go Back
der Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Operations Computing: The . . .
JJ II
hd(L) = 1 J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
JJ II
cons(0, L) = 0 :: L = [0, 1, 2, 3, 4] J I
Go Back
back to lists Recap
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Generating Primes
upto n Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Properties Computing: The . . .
Algorithms: Design & . . .
JJ II
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
√ a proper divisor d, 2 ≤ d ≤
has A large Example: . . .
Lecture-wise Index to . . .
JJ II
has a prime divisor p, 2 ≤ p ≤ b nc. J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Odd Primes Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
• An odd number > 1 is a prime iff it Compound Data
Higher Order Functions . . .
J I
• An odd number n > 1 is a prime iff Page 353 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
[(1, 2)] if n = 2
Home Page
JJ II
generateF rom otherwise
J I
([(1, 2)], 3, n, 2)
Page 354 of 644
where Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Invariant Imperative . . .
A large Example: . . .
Home Page
J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
generateF rom
Computing: The . . .
Algorithms: Design & . . .
P if m > n Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
generateF rom elseif
Home Page
JJ II
J I
generateF rom else
Page 356 of 644
(P, m + 2, n, k)
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primeW RT (m, P )
Computing: The . . .
Home Page
is prime with
√ respect to the list of all Full Screen
primes ≤ n Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primeW RT (m, P ) Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primeW RT Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
true if P = nil Imperative . . .
A large Example: . . .
primeW RT else Title Page
(m, tl(P ))
JJ II
J I
where Page 359 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Density of Primes Computing: The . . .
Algorithms: Design & . . .
Let π(n) denote the number of primes Introducing Reals
Correctness, . . .
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Prime Number
Theorem Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
π(n) Imperative . . .
limn→∞ n/ ln n = 1 A large Example: . . .
Lecture-wise Index to . . .
Title Page
at higher n J I
Go Back
Theory. Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Prime Number
Theorem
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
π(n) Correctness, . . .
JJ II
100000 9592 9.59% 1.104
J I
1000000 78, 498 7.85% 1.084 Page 362 of 644
Close
from David Burton: Elementary Number
Theory. Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
function calls
Lecture-wise Index to . . .
Home Page
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Diagnosis
For each m ≤ n,
Computing: The . . .
JJ II
should not be necessary
√ to check J I
each m more than π(b mc) times Page 364 of 644
Quit
ascending-order
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5.2. Compound Data & Lists
Computing: The . . .
1. Compound Data
Algorithms: Design & . . .
2. Recap: Tuples Introducing Reals
Correctness, . . .
3. Tuple: Formation
Compound Data
4. Tuples: Selection Higher Order Functions . . .
Imperative . . .
5. Tuples: Equality
A large Example: . . .
6. Tuples: Equality errors Lecture-wise Index to . . .
8. Lists: Append
Title Page
9. cons vs. @
JJ II
10. Lists of Functions
11. Lists of Functions J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. AS3: Complexity
19. Generating Primes: 2
20. primes2U pto(n)
Computing: The . . .
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
components. J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
Go Back
forward to Lists
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuple: Formation Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
val b = ("arun",true,2) J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuples: Selection
- #2 a; Computing: The . . .
- #1 a; Correctness, . . .
Compound Data
Higher Order Functions . . .
val it = "arun" : string Imperative . . .
A large Example: . . .
- #3 a; Lecture-wise Index to . . .
Title Page
- #4 a; JJ II
stdIn:1.1-1.5 Error: operator and ope J I
Close
(fn {4=4,...} => 4) a
Quit
- a = b; Compound Data
Higher Order Functions . . .
Home Page
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuples: Equality errors
- ("arun", (1, true)) Computing: The . . .
Algorithms: Design & . . .
("arun",(1,true)) JJ II
J I
= ("arun",1,true)
Page 372 of 644
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Lists: Recap
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
ponents JJ II
J I
new lists : Making new lists from old Page 373 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Append
- op @; Computing: The . . .
Algorithms: Design & . . .
Go Back
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
cons vs. @
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
as Title Page
L@M = JJ II
J I
M if L = nil
Page 375 of 644
h :: (T @M ) if L = h :: T Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Title Page
val add2 = fn : int -> int JJ II
- fun add3 x = x + 3; J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists of Functions Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
- fun addall x = [(add1 x), (add2 x), JJ II
val addall = fn : int -> int list J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Sequences Computing: The . . .
[] if n ≤ 0 Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
AS1(a, d, n − 1) else Home Page
@[a + (n − 1) ∗ d]
Title Page
JJ II
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tail Recursion Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
AS2(a, d, n) = Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
[] if n ≤ 0 A large Example: . . .
Lecture-wise Index to . . .
Home Page
JJ II
where J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tail Recursion:
Invariant Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
L if k ≥ n J I
Page 380 of 644
AS2 iter(a, d, n, k + 1 else Go Back
Full Screen
L@[a + k ∗ d])
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Complexity
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
@ n O(n) J I
Pn 2)
:: i=0 i O(n Page 381 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Another Tail Recursion
: AS3 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
AS3(a, d, n) = Correctness, . . .
Compound Data
Higher Order Functions . . .
[] if n ≤ 0 Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
where JJ II
Go Back
IN V 3 : L = (a + (n − 1) ∗ d) :: · · · :: Full Screen
· · · :: (a + (n0 − 1) ∗ d) :: L0 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Another Tail Recursion:
AS3 iter
Computing: The . . .
Algorithms: Design & . . .
IN V 3 : L = (a + (n − 1) ∗ d) :: · · ·
Introducing Reals
Correctness, . . .
Compound Data
L if n ≤ 0 JJ II
J I
Page 383 of 644
AS3 iter(a, d, n − 1, else Go Back
(a + (n − 1) ∗ d)::L)
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
@ 0 J I
P n
:: i=0 1 O(n) Page 384 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Generating Primes: 2
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
composites Lecture-wise Index to . . .
Home Page
• invariant JJ II
J I
• generateF rom Page 385 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
[(1, 2)] if n = 2
Home Page
JJ II
generate2F rom otherwise
J I
([(1, 2)], 3, n, 2)
Page 386 of 644
where Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Invariant Imperative . . .
A large Example: . . .
Home Page
J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
generate2F rom
Computing: The . . .
Algorithms: Design & . . .
P if m > n Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
generate2F rom elseif
Home Page
JJ II
J I
generate2F rom else
Page 388 of 644
(P, m + 2, n, k)
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
prime2W RT (m, P )
Computing: The . . .
Algorithms: Design & . . .
bound function length(P ) Introducing Reals
Correctness, . . .
JJ II
[(1, p1), . . . , (i − 1, pi−1)] J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
prime2W RT Computing: The . . .
Algorithms: Design & . . .
true if P = nil Higher Order Functions . . .
Imperative . . .
primeW RT else
Title Page
JJ II
(m, tl(P ))
J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
P n J I
prime2W RT m=3,odd(m) π(b mc) Page 391 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
primes2 : Diagnosis
Computing: The . . .
generate2F rom Algorithms: Design & . . .
Introducing Reals
J I
duce the complexity due to @?
Page 392 of 644
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5.3. Compound Data & List Algorithms
Computing: The . . .
1. Compound Data: Summary
Algorithms: Design & . . .
2. Records: Constructors Introducing Reals
Correctness, . . .
3. Records: Example 1
Compound Data
4. Records: Example 2 Higher Order Functions . . .
Imperative . . .
5. Records: Destructors
A large Example: . . .
6. Records: Equality Lecture-wise Index to . . .
8. Back to Lists
Title Page
9. Lists: Correctness
JJ II
10. Lists: Case Analysis
11. Lists: Correctness by Cases J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. List Functions: reverse2
19. List Functions:merge
20. List Functions:merge
Computing: The . . .
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Compound Data:
Summary Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
of element JJ II
J I
Records: Unordered named aggre- Page 395 of 644
Close
• Constructors & Destructors Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Constructors
Computing: The . . .
Home Page
field names JJ II
J I
types of fieldnames Page 396 of 644
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Example 1
Standard ML of New Jersey, Computing: The . . .
Algorithms: Design & . . .
name="Pinky"} JJ II
J I
: {age:int,
Page 397 of 644
fav_colour:string, Go Back
} Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Example 2
Computing: The . . .
Home Page
}; Title Page
- val billu = JJ II
{age=1,fav_colour="blue",name="Billu" J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Title Page
val it = "blue" : string JJ II
- #name billu; J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Records: Equality
Computing: The . . .
Home Page
}; Title Page
- val pinky2 = JJ II
{age=3,fav_colour="pink",name="Pinky" J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tuples & Records Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
J I
components are named rather than Page 401 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Back to Lists Computing: The . . .
Algorithms: Design & . . .
L = [] Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
f (L) =
g(h, T ) if L = h :: T Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Case Analysis
Computing: The . . .
inductive defns on lists Algorithms: Design & . . .
Introducing Reals
– [] Lecture-wise Index to . . .
Home Page
• ML provides convenient JJ II
J I
case analysis based on patterns. Page 404 of 644
fun f [] = c Go Back
Close
;
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Correctness by
Cases
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Lists-correctness Compound Data
Higher Order Functions . . .
JJ II
Go Back
P (h :: T ) Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
List-functions: length
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
length (h :: T ) = 1 + (length T ) JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
L Home Page
search (x, []) = f alse Title Page
J I
search (x, h :: T ) = search(x, T ) else
Page 407 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
JJ II
search2 (x, T )
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions: ordered Computing: The . . .
Algorithms: Design & . . .
Title Page
ordered [] JJ II
ordered [h]
J I
ordered (h0 :: h1 :: T ) if h0 ≤ h1 and Page 409 of 644
ordered(h1 :: T )
Go Back
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:insert
Given an ordered list L : α List, insert Computing: The . . .
Algorithms: Design & . . .
Home Page
insert (x, h :: T ) = x :: (h :: T ) Title Page
JJ II
if x ≤ h J I
Page 410 of 644
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
List Functions:
Algorithms: Design & . . .
Title Page
reverse [] = [] JJ II
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:
Computing: The . . .
Home Page
where
Title Page
rev ([], N ) = N JJ II
Correctness ?? Go Back
Time Complexity?? O(n) Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:merge
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
List Functions:merge Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
merge([], M ) = M Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
a :: (merge(S, M )) if a ≤ b
J I
Page 414 of 644
merge(L, M ) =
Go Back
b :: (merge(L, T )) else
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
L = a :: S JJ II
M = b :: T J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ML: merge
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
fun merge ([], M) = M Higher Order Functions . . .
Imperative . . .
if a <= b JJ II
J I
then a::merge(S, M)
Page 416 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sorting by Insertion
Computing: The . . .
Title Page
Hence sort[10, 8, 3, 6, 9, 7, 4, 8, 1] = JJ II
[1, 3, 4, 6, 7, 8, 8, 9, 10] J I
isort[] = [] Page 417 of 644
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sorting by Merging Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
msort [] = [] Higher Order Functions . . .
Imperative . . .
msort L = merge ((msort M ), Home Page
(msort N ))
Title Page
JJ II
where J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sorting by Merging Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
where Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
JJ II
where J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Functions as Data Computing: The . . .
Algorithms: Design & . . .
JJ II
type.
J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Higher Order Computing: The . . .
Algorithms: Design & . . .
Functions
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
structure. JJ II
J I
Functions may be defined with
Page 421 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
6. Higher Order Functions & Structured Data
6.1. Higher Order Functions
1. Summary: Compound Data Computing: The . . .
Algorithms: Design & . . .
2. List: Examples
Introducing Reals
5. An Example Imperative . . .
A large Example: . . .
6. Currying Lecture-wise Index to . . .
7. Currying: Contd
Home Page
8. Generalization
Title Page
9. Generalization: 2
10. Applying a list JJ II
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Efficient Generalization
19. Sequences: 2
20. More Generalizations
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Data Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
• Lists JJ II
– Correctness J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
JJ II
• Reversing a list J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Home Page
• Sorting by Divide-and-Conquer JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Functions
Lecture-wise Index to . . .
Home Page
• Functions as data
Title Page
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An Example Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
List of functions Compound Data
Higher Order Functions . . .
• add1 x = x + 1
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
• add3 x = x + 3 JJ II
Suppose we needed to define a long J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Currying Computing: The . . .
ML’s response :
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
- op +; J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Currying: Contd Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Generalization Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
[] if n ≤ 0 Home Page
JJ II
ML’s response :
J I
val list_adds = fn : Page 432 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Applying a list Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
addall Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
ML’s response: JJ II
J I
val applyl = fn :
Page 433 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
val interval = fn :
Title Page
- interval 53 5; J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Associativity Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
α → β → γ = α → (β → γ) Title Page
JJ II
If f : α → β → γ → δ J I
and f a b : γ → δ Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Apply to a list
Apply a list Transpose of a matrix Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
val it = fn : (’a -> ’b) -> Title Page
Full Screen
val it = [7.0,9.0,2.0,3.0]
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sequences Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Arithmetic sequences-1 Compound Data
Title Page
[] if n ≤ 0 JJ II
J I
a :: (map (addc d) else Page 437 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Further Generalization
Given Computing: The . . .
Algorithms: Design & . . .
Home Page
JJ II
(curry2 f ) : α → (α → α) J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Further Generalization
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
[] if n ≤ 0 Lecture-wise Index to . . .
Home Page
Title Page
a :: (map ((curry2 f ) d)
JJ II
(seq (f, a, d, n − 1))) else
J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Sequences Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
Harmonic: HS1(a, d, n) = JJ II
where
reci x = 1.0/(real x) gives the recipro- Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Efficient Generalization Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[] if n ≤ 0 Home Page
Title Page
J I
is the sequence of length n generated Page 441 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Generalizations Computing: The . . .
Home Page
quences JJ II
• ex,
J I
trigonometric functions upto Page 443 of 644
series Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
More Summations
Wasteful to first generate the se- Computing: The . . .
Xu Imperative . . .
A large Example: . . .
Home Page
Go Back
0 if [l, u] = ∅ Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Or Maybe . . . Products Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
i=l
JJ II
prod(f, succ, l, u) = J I
Page 445 of 644
1 if [l, u] = ∅ Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Or Some Other N
a ⊗ (b ⊗ c) = (a ⊗ b) ⊗ c Home Page
Title Page
a⊗e=a=e⊗a J I
u
O Go Back
Close
i=l
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
e if [l, u] = ∅ JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Examples of ⊗, e Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
6.2. Structured Data
1. Transpose of a Matrix
2. Transpose: 0
Computing: The . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. Constructors
20. Shapes
21. Shapes: Triangle Inequality
Computing: The . . .
30. Resistors JJ II
31. Resistors: Example
J I
32. Resistors: ML session
Page 450 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose of a Matrix Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Map Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
transpose L = JJ II
trans L if is2DM atrix(L) J I
⊥
Page 451 of 644
else Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 0
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ 11 12 13 ]
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ 21 22 23 ] Home Page
[ 31 33 ]
Title Page
32 JJ II
[ 41 42 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 10
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ 11 12 13 ]
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ 21 22 23 ] Home Page
[ 31 33 ]
Title Page
32 JJ II
[ 41 42 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 01
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ 12 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ 22 23 ] Home Page
[ 33 ]
Title Page
32 JJ II
[ 42 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 20
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ 12 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ 22 23 ] Home Page
[ 33 ]
Title Page
32 JJ II
[ 42 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 02
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ 23 ] Home Page
[ 12 22 32 42 ]
[ 33 ]
Title Page
JJ II
[ 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 30
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ 13 ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ 23 ] Home Page
[ 12 22 32 42 ]
[ 33 ]
Title Page
JJ II
[ 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Transpose: 03
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
[ [ Correctness, . . .
Compound Data
Higher Order Functions . . .
[ ] [ 11 21 31 41 ] Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
[ ] Home Page
[ 12 22 32 42 ]
[ ]
Title Page
JJ II
[ ] [ 13 23 33 43 ] J I
] ] Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
trans [] = [] Higher Order Functions . . .
Imperative . . .
trans [] :: T L = []
A large Example: . . .
Lecture-wise Index to . . .
trans LL = (map hd LL) :: Home Page
JJ II
and J I
where Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
is2DM atrix Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
dimensions [H] =
A large Example: . . .
Lecture-wise Index to . . .
Title Page
dimensions (H :: T L) = JJ II
J I
(b and (h = c), r + 1, c)
Page 460 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
erogeneous data. J I
desperate!)
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Enumeration Types
Many languages allow user-defined Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
J I
– colours Page 462 of 644
Close
desperate!) Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
User Defined
Structural Types Computing: The . . .
Algorithms: Design & . . .
Many languages allow user-defined Introducing Reals
Correctness, . . .
JJ II
Go Back
– trees Full Screen
– graphs Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
data JJ II
J I
– Functions as data Page 464 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Data as 0-ary Functions Computing: The . . .
Algorithms: Design & . . .
ments Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
where JJ II
f : α → int J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
naming JJ II
composition J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists as Structured
Data
Computing: The . . .
nil | Correctness, . . .
Compound Data
Higher Order Functions . . .
cons of ’a * ’a list Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
J I
| : or (alternative) Page 468 of 644
Quit
structor cons • •
First• • •
Prev Next Last Go Back •Full Screen •Close •Quit
Constructors Computing: The . . .
Algorithms: Design & . . .
Title Page
and α list JJ II
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shapes
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
J I
| TRIANGLE of
Page 470 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shapes: Triangle Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Inequality Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
(b+c>a) andalso
J I
(c+a>b) Page 471 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shapes: Area Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
exception notShape; Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
3.14159 * r * r Title Page
l*b J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Shapes: Area
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
in Math.sqrt Title Page
(s*(s-a)*(s-b)*(s-c)) JJ II
end J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ML: Try out
- use "shapes.sml"; Computing: The . . .
Algorithms: Design & . . .
J I
val isTriangle =
Page 474 of 644
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
ML: Try out (contd.)
Computing: The . . .
Home Page
- area JJ II
- Go Back
Full Screen
Close
Back to User defined types
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Enumeration Types Computing: The . . .
Title Page
| WED | THU | FRI;
JJ II
datatype weekends = SAT | SUN J I
| weekends; Go Back
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Recursive Data Types Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
J I
a case-analysis of the structure (ba- Page 477 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: Equivalent Computing: The . . .
Algorithms: Design & . . .
J I
in (r1*r2)/(r1+r2)
Page 479 of 644
end; Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors
5.0 Computing: The . . .
Algorithms: Design & . . .
5.0 2.0
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
4.0
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
+ − Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: Example
val R = PAR(
SER( Computing: The . . .
Algorithms: Design & . . .
), Home Page
RES (5.0), JJ II
RES (2.0) J I
), Full Screen
RES(3.0) Close
); Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: ML session
- use "resistors.sml";
Computing: The . . .
[opening resistors.sml] Algorithms: Design & . . .
Introducing Reals
JJ II
val it = () : unit
J I
- value R; Page 482 of 644
- Full Screen
Close
Quit
Resistance Expressions
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
6.3. User Defined Structured Data Types
Computing: The . . .
1. User Defined Types
Algorithms: Design & . . .
2. Resistors: Grouping Introducing Reals
Correctness, . . .
3. Resistors: In Pairs
Compound Data
4. Resistor: Values Higher Order Functions . . .
Imperative . . .
5. Resistance Expressions
A large Example: . . .
6. Resistance Expressions Lecture-wise Index to . . .
8. Arithmetic Expressions: 0
Title Page
9. Arithmetic Expressions: 1
JJ II
10. Arithmetic Expressions: 2
11. Arithmetic Expressions: 3 J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Arithmetic Expressions: 0
19. Trees: Traversals
20. Recursive Data Types: Correctness
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
User Defined Types
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
∗ Non-recursive JJ II
∗ Enumeration Types J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistors: Grouping
Computing: The . . .
5.0
Correctness, . . .
Compound Data
5.0 2.0
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
R1 JJ II
J I
3.0 R4
Page 486 of 644
Go Back
Full Screen
+ − Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
val R3 = SER (R1, R2); JJ II
val R4 = PAR (R3, RES(3.0)); J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistor: Values Computing: The . . .
Algorithms: Design & . . .
- value R4; J I
- Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistance
Expressions
Computing: The . . .
A resistance expression Algorithms: Design & . . .
Introducing Reals
PAR Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
SER
Lecture-wise Index to . . .
Home Page
Title Page
PAR SER JJ II
J I
Full Screen
5 4 5 2 3 Close
Quit
Circuit Diagram
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resistance
Expressions
A resistance expression Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
R4 PAR Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
R3 SER
Lecture-wise Index to . . .
Home Page
Title Page
R1 PAR R2 SER JJ II
J I
Full Screen
5 4 5 2 3 Close
Quit
•First Circuit
•Prev •NextDiagram
•Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Title Page
((5 * ˜4) + ˜(5 - 2)) div 3
JJ II
are represented as trees J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 0 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
div Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
+
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
* ~
J I
5 ~ 3 Go Back
Full Screen
4 5 2 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 1 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
div Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
+ Lecture-wise Index to . . .
Home Page
Title Page
JJ II
* ~
J I
5 ~ 3 Go Back
Full Screen
4 5 2 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .
+
Home Page
Title Page
JJ II
J I
* ~
Page 494 of 644
3
Go Back
5 −4 3 Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .
+ Home Page
Title Page
JJ II
~
J I
*
Page 495 of 644
3 Go Back
5 −4 3 Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .
+
Home Page
Title Page
JJ II
J I
−20 −3
Page 496 of 644
Go Back
3 Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .
+
Home Page
Title Page
JJ II
J I
−20 −3
Page 497 of 644
Go Back
3 Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic Computing: The . . .
Home Page
−23
Title Page
JJ II
J I
Go Back
3 Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 7
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
−23
Title Page
JJ II
J I
Go Back
3 Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
−8 JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Binary Trees
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
Node of ’a * Title Page
’a bintree * JJ II
’a bintree J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arithmetic
Expressions: 0 Computing: The . . .
Algorithms: Design & . . .
Arithmetic Expressions Introducing Reals
Correctness, . . .
Title Page
* ~ JJ II
J I
~ − 3
Page 502 of 644
5
Go Back
Full Screen
4 5 2 Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Trees: Traversals
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
• inorder
Title Page
JJ II
• postorder J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Title Page
Correctness on lists by cases
JJ II
P is proved by case analysis. J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Data Types: Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
P (T ) for all elements of the data
J I
type less than a certain depth Page 505 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7. Imperative Programming: An Introduction
7.1. Introducing a Memory Model
1. Summary: Functional Model Computing: The . . .
Algorithms: Design & . . .
2. CPU & Memory: Simplified
Introducing Reals
7. Memory
Home Page
8. The Imperative Model
Title Page
9. State Changes: σ
10. State JJ II
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Imperative vs Functional Variables
19. Assignment Commands
20. Assignment Commands
Computing: The . . .
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Summary: Functional
Model Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
J I
– Every complex piece of data is Page 508 of 644
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CPU & Memory:
Simplified Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Home Page
Title Page
Peripherals JJ II
CPU
J I
Printer
Disk
Page 509 of 644
Go Back
Screen Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Resource
Management Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Home Page
Peripherals
Title Page
JJ II
CPU Printer
J I
Disk
Page 510 of 644
Keyboard
Go Back
Screen
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Shell: User Interface Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Memory Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Peripherals
Home Page
Title Page
CPU Printer
Disk JJ II
Keyboard J I
Screen
Page 511 of 644
Full Screen
Shell Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
GUI: User Interface Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Memory Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Keyboard JJ II
Screen
J I
Shell Go Back
Full Screen
Graphical User Interface (GUI)
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Memory Model:
Simplified Computing: The . . .
Algorithms: Design & . . .
Title Page
or string JJ II
Go Back
k) Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Memory
0 1 2 3 Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
32K−1 Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
J I
– state : Addresses → V alues
Page 515 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
4
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
true JJ II
J I
"#a" Go Back
Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
State
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
• σ(20) = null
Title Page
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
be changed at a time. J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
4
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
true JJ II
J I
"#a" Go Back
Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ1
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
5 Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
true JJ II
J I
"#a" Go Back
Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
State Changes: σ2
0 1 2 3
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
5 Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Title Page
true JJ II
J I
12 "#a" Go Back
Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Languages
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Peripherals
Home Page
Title Page
CPU Printer
JJ II
Disk
J I
Keyboard
Page 522 of 644
Screen
Go Back
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
User Programs
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Disk
JJ II
Keyboard
J I
Screen
Page 523 of 644
Full Screen
Programming Language Interface
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative Languages Computing: The . . .
Title Page
mapped to an address . J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Commands Correctness, . . .
Compound Data
Higher Order Functions . . .
JJ II
J I
x 1 y 2 Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Commands Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Store the value 5 in x. A large Example: . . .
Lecture-wise Index to . . .
x := 5 Home Page
Title Page
JJ II
J I
x 5 y 2
Page 527 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Commands Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Copy the value contained in y into x. A large Example: . . .
Lecture-wise Index to . . .
x :=!y Home Page
Title Page
JJ II
J I
x 2 y 2
Page 528 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Home Page
. Title Page
JJ II
J I
x 3 y 2 Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment Computing: The . . .
Algorithms: Design & . . .
Commands
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
in y. Lecture-wise Index to . . .
Home Page
JJ II
J I
x 3 y 6 Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Assignment
Commands: Swap Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
ously! J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Swap Computing: The . . .
Algorithms: Design & . . .
Title Page
val t = ref 0; JJ II
t := !x; J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Swap
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Home Page
x := !y; JJ II
y := !t; J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Swap
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Title Page
val t = !x; JJ II
x := !y; J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7.2. Imperative Programming:
Computing: The . . .
1. Imperative vs Functional
Algorithms: Design & . . .
2. Features of the Store Introducing Reals
Correctness, . . .
3. References: Experiments
Compound Data
4. References: Experiments Higher Order Functions . . .
Imperative . . .
5. References: Experiments
A large Example: . . .
6. Aliases Lecture-wise Index to . . .
8. References: Aliases
Title Page
9. References: Experiments
JJ II
10. After Garbage Collection
11. Side Effects J I
12. Imperative ML
Page 535 of 644
13. Imperative ML
Go Back
14. Imperative ML
15. Imperative ML Full Screen
17. Imperative ML
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Imperative ML
19. Common Errors
20. Aliasing & References
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
• Imperative Model JJ II
J I
• State Changes Page 537 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
Dereferencing ! : α ref → α JJ II
J I
Updation :=: α ref ∗ α → unit Page 538 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References:
Experiments
Computing: The . . .
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
a •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References:
Experiments
Computing: The . . .
Title Page
JJ II
0 J I
Go Back
Full Screen
Close
Quit
a b •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
References: Correctness, . . .
Compound Data
- a = b; Home Page
- !a = !b; JJ II
J I
val it = true : bool
Page 541 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Aliases
- val x = ref 0; Computing: The . . .
Algorithms: Design & . . .
0 A large Example: . . .
Lecture-wise Index to . . .
Home Page
0 Title Page
0 JJ II
J I
Go Back
Full Screen
Close
a b x Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References:
Experiments
Computing: The . . .
Title Page
0 JJ II
0 J I
Go Back
Full Screen
Close
Quit
a b x y •First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
References: Aliases
- x := !x + 1; Computing: The . . .
Algorithms: Design & . . .
0 A large Example: . . .
Lecture-wise Index to . . .
Home Page
1 Title Page
0 JJ II
J I
Go Back
Full Screen
Close
a b x y Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
References: Correctness, . . .
Compound Data
- x = y; JJ II
J I
val it = true : bool
Page 545 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
After Garbage
Collection Computing: The . . .
Algorithms: Design & . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
a b x y Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Side Effects Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
value Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
JJ II
• But side-effects are compatible with
J I
functional programming since it is Page 547 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
location J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to Computing: The . . .
Algorithms: Design & . . .
JJ II
perative variables J I
location Go Back
Full Screen
automatically Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to Computing: The . . .
Algorithms: Design & . . .
JJ II
izations J I
cell Go Back
Full Screen
automatically Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to Computing: The . . .
Algorithms: Design & . . .
JJ II
guage programs J I
cell Go Back
Full Screen
automatically Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Nasty Surprises
Separation of declaration from initial- Computing: The . . .
Algorithms: Design & . . .
Title Page
memory location contains some JJ II
data J I
Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
cell J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative ML
• Does not provide direct access to
Computing: The . . .
J I
automatically and securely Page 554 of 644
guages Close
Quit
Common Errors
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Aliasing & References Computing: The . . .
Algorithms: Design & . . .
Before deallocation:
0 1 2 3
Introducing Reals
Correctness, . . .
Compound Data
0 0 1 Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
a b x y
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Dangling References
Deallocate x through a system call Computing: The . . .
0 1 2 3
Algorithms: Design & . . .
0 0 Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
a b y Go Back
Full Screen
Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
New Reference
val z = ref 12;
Computing: The . . .
Algorithms: Design & . . .
0 1 2 3 Introducing Reals
0 0 12 Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
a b y z Full Screen
Close
Quit
By sheer coincidence !y =• 12• First Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
J I
Assignment
Page 559 of 644
print Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Imperative Commands: Computing: The . . .
JJ II
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Predefined Compound
Commands Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7.3. Arrays
Computing: The . . .
1. Why Imperative
Algorithms: Design & . . .
2. Arrays Introducing Reals
Correctness, . . .
3. Indexing Arrays
Compound Data
4. Indexing Arrays Higher Order Functions . . .
Imperative . . .
5. Indexing Arrays
A large Example: . . .
8. 2D Arrays
Title Page
9. 2D Arrays: Indexing
JJ II
10. Ordering of indices
11. Arrays vs. Lists J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Why Imperative Computing: The . . .
Algorithms: Design & . . .
JJ II
– numerical packages J I
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arrays
An array of length n is a contiguous
Computing: The . . .
Home Page
Title Page
C0 Ci
JJ II
Cn−1 J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Indexing Arrays
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Home Page
J I
from C0 Page 565 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Indexing Arrays Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
C0 Ci
A large Example: . . .
Lecture-wise Index to . . .
Title Page
JJ II
J I
Go Back
Full Screen
a0 a n−1 ai Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
JJ II
• The address ai of cell Ci is J I
ai = a0 + i Page 567 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Physical Addressing
If each element occupies s physical Computing: The . . .
Algorithms: Design & . . .
ai = a0 + i × s Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
a0 a n−1 ai Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arrays
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
numbered 0 to c − 1 JJ II
J I
is also a contiguous sequence of rc
Page 569 of 644
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2D Arrays
A 2 dimensional-array is represented Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
given by JJ II
aij = a00 + (c × i + j) J I
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
2D Arrays: Indexing Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
J I
(i + 1, 0) if j = n − 1 Page 571 of 644
succ(i, j) =
(i, j + 1) else Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
JJ II
(i < k) or J I
(i = k and j < l) Page 572 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Arrays: Physical
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
a0 a n−1 ai
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Lists: Physical
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
8. A large Example: Tautology Checking
Computing: The . . .
Algorithms: Design & . . .
8.1. Large Example: Tautology Checking
Introducing Reals
1. Logical Arguments Correctness, . . .
Compound Data
2. Saintly and Rich
Higher Order Functions . . .
3. About Cats Imperative . . .
A large Example: . . .
4. About God
Lecture-wise Index to . . .
5. Russell’s Argument
Home Page
6. Russell’s Argument
7. Russell’s Argument Title Page
8. Russell’s Argument JJ II
9. Propositions
J I
10. Compound Propositions
Page 576 of 644
11. Valuations
12. Valuations Go Back
14. Properties
Close
15. Negation Normal Form
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. Literals & Clauses
17. Conjunctive Normal Form
18. Validity
Computing: The . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Logical Arguments
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Examples. Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
• About cats
Title Page
JJ II
• About God J I
• Russell’s argument
Page 578 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Title Page
hy2 One cannot be both saintly and rich.
JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Home Page
J I
hy4 Cats eat meat. Page 580 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
About God
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Title Page
evil. J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .
Algorithms: Design & . . .
hy1 If we can directly know that God ex- Introducing Reals
Correctness, . . .
experience. Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
J I
hy3 If we can know that God exists, then Page 582 of 644
ists. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .
Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .
hy1 If we can directly know that God ex- Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
ists, then we can know God exists by Compound Data
Higher Order Functions . . .
experience. Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
J I
hy3 If we can know that God exists, then Page 584 of 644
ists). Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Russell’s Argument Computing: The . . .
Home Page
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
false. J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Compound
Propositions Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound propositions may be Compound Data
Higher Order Functions . . .
Home Page
JJ II
not ¬ J I
or ∨ Go Back
equivalent ⇐⇒ Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Valuations
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
p ¬p JJ II
true false J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
false true false true true false
J I
false false false false true true Page 589 of 644
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tautology
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
JJ II
• p∨¬p J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Properties
• Every proposition may be ex- Computing: The . . .
Algorithms: Design & . . .
JJ II
to push ¬ inward
Page 591 of 644
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
of atoms. Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Wm JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Conjunctive Normal
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Form
Correctness, . . .
Compound Data
Higher Order Functions . . .
Imperative . . .
Title Page
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Validity
Introducing Reals
Correctness, . . .
Compound Data
Higher Order Functions . . .
Home Page
hypotheses. Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
JJ II
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Validity & Tautologies Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
JJ II
• If it is not a tautology, to deter-
J I
mine what truth assignments to the Page 598 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tautology Checking Computing: The . . .
Algorithms: Design & . . .
Vn
A proposition in CNF ( i=1pi) Introducing Reals
Correctness, . . .
Compound Data
proposition pi , 1 ≤ i ≤ m, is a tau-
A large Example: . . .
Lecture-wise Index to . . .
Title Page
must be false J I
Wm
• Clause pi = j=1lij is false
Page 599 of 644
Go Back
m is false Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Falsifying Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Vn
For a proposition in CNF ( i=1pi) that Correctness, . . .
Compound Data
Home Page
argument JJ II
J I
– sets the atoms that occur nega- Page 600 of 644
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
8.2. Tautology Checking Contd.
Computing: The . . .
1. Tautology Checking
Algorithms: Design & . . .
2. Normal Forms Introducing Reals
Correctness, . . .
3. Top-down Development
Compound Data
4. The Signature Higher Order Functions . . .
Imperative . . .
5. The Core subproblem
A large Example: . . .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Home Page
• Compound Propositions JJ II
J I
• Truth table Page 602 of 644
• Tautologies Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Normal Forms
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Compound Data
Home Page
J I
tautologies Page 603 of 644
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Top-down
Development Computing: The . . .
Algorithms: Design & . . .
Title Page
JJ II
tautology
Page 604 of 644
Go Back
false Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Signature
signature PropLogic = Computing: The . . .
Algorithms: Design & . . .
val Valid: JJ II
J I
Argument -> bool *
Page 605 of 644
end; Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Title Page
Form (NNF) J I
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The datatype Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
datatype Prop = Compound Data
Higher Order Functions . . .
Home Page
AND of Prop * Prop |
Title Page
OR of Prop * Prop | JJ II
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Convert to CNF Computing: The . . .
Algorithms: Design & . . .
Title Page
where JJ II
J I
Full Screen
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Home Page
double negations. J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
⇐⇒ and ⇒ Elimination Computing: The . . .
Algorithms: Design & . . .
J I
IMP (Q, P)))
Page 610 of 644
| ... Go Back
∨. Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Computing: The . . .
Algorithms: Design & . . .
Title Page
NOT (ATOM a) JJ II
| nnf (NOT (NOT (P))) = J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Push ¬ inward Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Title Page
nnf (AND (NOT (P),
JJ II
NOT (Q))) J I
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
conj of disj Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
fun conj_of_disj (AND (P, Q)) = Compound Data
Higher Order Functions . . .
Home Page
| conj_of_disj (OR (P, Q)) =
Title Page
conj_of_disj (Q)) J I
Go Back
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Push ∨ inward Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Correctness, . . .
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Tautology &
Falsification Computing: The . . .
Algorithms: Design & . . .
Introducing Reals
Home Page
Go Back
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. Lecture-wise Index to Slides Computing: The . . .
Compound Data
1. Introduction
Higher Order Functions . . .
2. Computing tools Imperative . . .
3. Ruler and Compass A large Example: . . .
8. Primitives
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. Primitive expressions
10. Methods of combination
11. Methods of abstraction
Computing: The . . .
12. The Functional Model Algorithms: Design & . . .
Compound Data
15. Mathematical Notation 3: Factorial
Higher Order Functions . . .
16. A Functional Program: Factorial
Imperative . . .
17. A Computation: Factorial A large Example: . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4. SML: Primitive Integer Operations 1
5. SML: Primitive Integer Operations 1
6. SML: Primitive Integer Operations 1
Computing: The . . .
7. SML: Primitive Integer Operations 1 Algorithms: Design & . . .
8. SML: Primitive Integer Operations 2 Introducing Reals
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
26. Fibonacci Numbers
27. Euclidean Algorithm
Technical Completeness & Algorithms (61-92) Computing: The . . .
A large Example: . . .
6. Powering: Math
Lecture-wise Index to . . .
7. Powering: SML
8. Technical completeness Home Page
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. Integer Square Root 1
20. Integer Square Root 2
21. An analysis
Computing: The . . .
22. Algorithmic idea Algorithms: Design & . . .
Compound Data
25. SML: shrink
Higher Order Functions . . .
26. SML: intsqrt
Imperative . . .
27. Run it! A large Example: . . .
2. Recap: Power
Go Back
3. Recap: Technical completeness
4. Recap: More Algorithms Full Screen
6. Intsqrt: Reorganized
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7. Some More Algorithms
8. Combinations: Math
9. Combinations: Details
Computing: The . . .
10. Combinations: SML Algorithms: Design & . . .
11. Perfect Numbers Introducing Reals
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
29. Algorithmic Variations
30. Algorithmic Variations
Variations: Algorithms & Code (123-149) Computing: The . . .
5. Combinations 3 Imperative . . .
A large Example: . . .
6. Perfect 2
Lecture-wise Index to . . .
7. Power 2
8. A Faster Power Home Page
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. Back to Math
20. Incorrectness
21. ifdivisor3
Computing: The . . .
22. Run it! Algorithms: Design & . . .
Compound Data
25. The n’s
Higher Order Functions . . .
26. Scope
Imperative . . .
27. Scope Rules A large Example: . . .
3. Overlapping Scopes
JJ II
4. Spannning
5. Scope & Names J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
12. Names & References
13. Names & References
14. Names & References
Computing: The . . .
15. Names & References Algorithms: Design & . . .
Compound Data
18. local...in...end
Higher Order Functions . . .
19. local...in...end
Imperative . . .
20. local...in...end A large Example: . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Floating Point (182-201)
1. So Far-1: Computing
2. So Far-2: Algorithms & Programs Computing: The . . .
Correctness, . . .
5. So far-5: Caveats
Compound Data
6. So Far-6: Algorithmic Variations Higher Order Functions . . .
7. So Far-7: Computations Imperative . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Root Finding, Composition and Recursion (202-229)
1. Root Finding: Newton’s Method
2. Root Finding: Newton’s Method Computing: The . . .
Correctness, . . .
5. Root Finding: Newton’s Method
Compound Data
6. Root Finding: Newton’s Method Higher Order Functions . . .
7. Newton’s Method: Basis Imperative . . .
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
21. Two Computations of h(1)
22. Two Computations of h(−1)
23. Recursive Computations
Computing: The . . .
24. Recursion: Left Algorithms: Design & . . .
Compound Data
27. Some Practical Questions
Higher Order Functions . . .
28. Some Practical Questions
Imperative . . .
Lecture-wise Index to . . .
1. Recursion Revisited
2. Linear Recursion: Waxing Home Page
4. Nonlinear Recursions
JJ II
5. Fibonacci: contd
6. Recursion: Waxing & Waning J I
8. Non-termination
Go Back
9. Termination
10. Proofs of termination Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
13. Proof of termination: Factorial
14. Fibonacci: Termination
15. GCD computations
Computing: The . . .
16. Well-foundedness: GCD Algorithms: Design & . . .
17. Well-foundedness Introducing Reals
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
35. Factorial: Space
36. Fibonacci: Space
37. Fibonacci: Space
Computing: The . . .
Introducing Reals
1. Recapitulation
Correctness, . . .
2. Recapitulation Compound Data
3. Time & Space Complexity Higher Order Functions . . .
A large Example: . . .
5. Time Complexity
Lecture-wise Index to . . .
6. isqrt: Time Complexity
7. isqrt2: Time Home Page
13. Comparisons
Go Back
14. Efficiency Measures: Time
15. Efficiency Measures: Space Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
18. Factoring out calculations
19. Tail Recursion: 1
20. Tail Recursion: 2
Computing: The . . .
21. Factorial: Tail Recursion Algorithms: Design & . . .
Compound Data
24. Factorial: Issues
Higher Order Functions . . .
25. Fibonacci: Tail Recursion
Imperative . . .
26. Fibonacci: Tail Recursion A large Example: . . .
4. Comparisons
Go Back
5. Transformation Issues
6. Correctness Issues 1 Full Screen
8. Correctness Theorem
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. Invariants & Correctness 1
10. Invariants & Correctness 2
11. Invariance Lemma: f actL tr
Computing: The . . .
12. Invariance: Example Algorithms: Design & . . .
Compound Data
15. Invariance Lemma: f ib iter
Higher Order Functions . . .
16. Proof
Imperative . . .
17. Correctness: Fibonacci A large Example: . . .
3. Tuples Close
4. Lists
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
5. New Lists
6. List Operations
7. List Operations: cons
Computing: The . . .
8. Generating Primes upto n Algorithms: Design & . . .
Compound Data
11. Odd Primes
Higher Order Functions . . .
12. primesU pto(n)
Imperative . . .
13. generateF rom(P, m, n, k) A large Example: . . .
2. Recap: Tuples
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
3. Tuple: Formation
4. Tuples: Selection
5. Tuples: Equality
Computing: The . . .
6. Tuples: Equality errors Algorithms: Design & . . .
7. Lists: Recap Introducing Reals
24. prime2W RT
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
25. primes2: Complexity
26. primes2: Diagnosis
Compound Data & List Algorithms (368-394) Computing: The . . .
A large Example: . . .
6. Records: Equality
Lecture-wise Index to . . .
7. Tuples & Records
8. Back to Lists Home Page
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
19. List Functions:merge
20. List Functions:merge
21. List Functions:merge contd.
Computing: The . . .
22. ML: merge Algorithms: Design & . . .
Compound Data
25. Sorting by Merging
Higher Order Functions . . .
26. Functions as Data
Imperative . . .
27. Higher Order Functions A large Example: . . .
3. Lists: Sorting
JJ II
4. Higher Order Functions
5. An Example J I
7. Currying: Contd
Go Back
8. Generalization
9. Generalization: 2 Full Screen
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
12. Associativity
13. Apply to a list
14. Sequences
Computing: The . . .
15. Further Generalization Algorithms: Design & . . .
Compound Data
18. Efficient Generalization
Higher Order Functions . . .
19. Sequences: 2
Imperative . . .
20. More Generalizations A large Example: . . .
2. Transpose: 0
Go Back
3. Transpose: 10
4. Transpose: 01 Full Screen
5. Transpose: 20 Close
6. Transpose: 02
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
7. Transpose: 30
8. Transpose: 03
9. trans
Computing: The . . .
10. is2DM atrix Algorithms: Design & . . .
11. User Defined Types Introducing Reals
20. Shapes JJ II
21. Shapes: Triangle Inequality
J I
22. Shapes: Area
23. Shapes: Area Page 637 of 644
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
29. Resistors: Equivalent
30. Resistors
31. Resistors: Example
Computing: The . . .
32. Resistors: ML session Algorithms: Design & . . .
Correctness, . . .
1. User Defined Types Compound Data
2. Resistors: Grouping Higher Order Functions . . .
A large Example: . . .
4. Resistor: Values
Lecture-wise Index to . . .
5. Resistance Expressions
6. Resistance Expressions Home Page
8. Arithmetic Expressions: 0
JJ II
9. Arithmetic Expressions: 1
10. Arithmetic Expressions: 2 J I
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
17. Binary Trees
18. Arithmetic Expressions: 0
19. Trees: Traversals
Computing: The . . .
20. Recursive Data Types: Correctness Algorithms: Design & . . .
Correctness, . . .
Introducing a Memory Model (473-499)
Compound Data
1. Summary: Functional Model Higher Order Functions . . .
A large Example: . . .
3. Resource Management
Lecture-wise Index to . . .
4. Shell: User Interface
5. GUI: User Interface Home Page
7. Memory
JJ II
8. The Imperative Model
9. State Changes: σ J I
15. Languages
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
16. User Programs
17. Imperative Languages
18. Imperative vs Functional Variables
Computing: The . . .
19. Assignment Commands Algorithms: Design & . . .
Compound Data
22. Assignment Commands
Higher Order Functions . . .
23. Assignment Commands
Imperative . . .
24. Assignment Commands: Swap A large Example: . . .
4. References: Experiments
Go Back
5. References: Experiments
6. Aliases Full Screen
8. References: Aliases
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
9. References: Experiments
10. After Garbage Collection
11. Side Effects
Computing: The . . .
12. Imperative ML Algorithms: Design & . . .
Compound Data
15. Imperative ML
Higher Order Functions . . .
16. Nasty Surprises
Imperative . . .
17. Imperative ML A large Example: . . .
2. Arrays Close
3. Indexing Arrays
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
4. Indexing Arrays
5. Indexing Arrays
6. Physical Addressing
Computing: The . . .
7. Arrays Algorithms: Design & . . .
Compound Data
10. Ordering of indices
Higher Order Functions . . .
11. Arrays vs. Lists
Imperative . . .
12. Arrays: Physical A large Example: . . .
6. Russell’s Argument
Go Back
7. Russell’s Argument
8. Russell’s Argument Full Screen
9. Propositions Close
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
11. Valuations
12. Valuations
13. Tautology
Computing: The . . .
14. Properties Algorithms: Design & . . .
Compound Data
17. Conjunctive Normal Form
Higher Order Functions . . .
18. Validity
Imperative . . .
19. Logical Validity A large Example: . . .
3. Top-down Development
Go Back
4. The Signature
5. The Core subproblem Full Screen
7. Convert to CNF
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
8. Rewrite into NNF
9. ⇐⇒ and ⇒ Elimination
10. Push ¬ inward
Computing: The . . .
11. Push ¬ inward Algorithms: Design & . . .
12. conj of disj Introducing Reals
Correctness, . . .
13. Push ∨ inward
Compound Data
14. Tautology & Falsification Higher Order Functions . . .
Imperative . . .
A large Example: . . .
Lecture-wise Index to . . .
Home Page
Title Page
JJ II
J I
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit