11-1
11. Computability and Continuity
Continuity becomes an important consideration for computing on
innite data such as streams.
Denitions.
An initial segment of a stream s = a1 , a2 , . . . is an initial (nite)
tuple of s: a1 , . . . , an (for some n).
Homework 4
Printed submission due Friday, November 18th in lecture
For this homework, produce a single .hs file containing all of your code
and print it for submission. You do not need to include a main; the code
can be tested in the GHC interpreter using
Homework 5
Printed submission due Monday, November 28th in lecture
1
Call-by-name semantics of FUN
Modify one rule from the semantics of FUN in a way that would make the
semantics call-by-name.
Hint: compare this to the modification given in the notes to
3-1
Polymorphism
[Reade ch. 2]
Consider:
fun I x = x;
Q. What is its type?
A. I : , for any type .
Introduce type variables , , , . . .
(Represented in ASCII by a, b, c, . . . .)
We get polymorphic types.
Examples:
Denition
Type
fun I x = x
I :
fun fst (x
5-1
5. Lazy and Eager Evaluation
[ Reade Chs. 8, 9 ]
Order of evaluation of an expression usually makes no dierence; e.g.:
Example 1.
let val x = 5 + (4 2)
in (4 x) + (3 - x)
end;
If evaluate expr. after substitution: lazy.
If not: eager or strict.
In gen
7-1
7. Higher Order Operations on Lists
[ Reade 3.4 ]
Examples.
(1) To double the values of a list of integers:
e.g. double list [3, 1, 4] = [6, 2, 8].
Def. by list recursion:
fun double list (a:x) = (2 a):(double list x)
| double list [ ] = [ ];
Type?
W
8-1
8. Concrete Data Types
[ Reade ch. 5 ]
We can dene new types (data types) with constructors.
(Dont need selectors!)
Then we can dene functions on those data types
recursion on construction of data types or
structural recursion on data types or
data
11-1
11. Computability and Continuity
Continuity becomes an important consideration for computing on
innite data such as streams.
Denitions.
An initial segment of a stream s = a1 , a2 , . . . is an initial (nite)
tuple of s: a1 , . . . , an (for some n).
3-1
Polymorphism
[Reade ch. 2]
Consider:
fun I x = x;
Q. What is its type?
A. I : , for any type .
Introduce type variables , , , . . .
(Represented in ASCII by a, b, c, . . . .)
We get polymorphic types.
Examples:
Denition
Type
fun I x = x
I :
fun fst (x
2-1
Recursive Denitions
Example 1. Factorial function:
fun fact n = if n = 0
then 1
else n * fact (n-1);
val fact = fn : int int
Alt.: using pattern matching (p.m.)
(Important method!)
fun fact 0 = 1
| fact n = n * fact (n-1);
Note. This denes a functio
1-1
Intro: Why Functional Programming?
In procedural/imperative languages (Pascal, C, Java, . . . )
(based on Von Neumann architecture) the basic unit is the assignment:
get side eects: expression has dierent values each time evaluated,
depending on the
8-1
8. Concrete Data Types
[ Reade ch. 5 ]
We can dene new types (data types) with constructors.
(Dont need selectors!)
Then we can dene functions on those data types
recursion on construction of data types or
structural recursion on data types or
data
12-1
12. Two Paradigms of Programming Languages:
Imperative and Functional
Interpreting an Imperative Language in SML
An imperative programming language (Pascal, C, Java, . . . ) has, as
its basic command, the assignment X := E.
A program in such a langua
10-1
10. More Stream Operations
Example: Stream of primes
As preparation, we dene the integer square root function:
fun sqrt 0 = 0
| sqrt n = let val k = sqrt (n 1) in
if (k + 1) (k + 1) > n
then k
else k + 1
end;
. . . and the prime number predicate:
f
9-1
9. Streams (Innite lists)
[A&S, 1st ed. 3.4; 2nd ed. 3.5; Paulson, Ch.5 (sequences) ]
To implement streams or innite lists in SML, we must simulate
lazy evaluation.
The idea is to code or store unevaluated expressions as functions.
To delay evaluation
7-1
7. Higher Order Operations on Lists
[ Reade 3.4 ]
Examples.
(1) To double the values of a list of integers:
e.g. double list [3, 1, 4] = [6, 2, 8].
Def. by list recursion:
fun double list (a:x) = (2 a):(double list x)
| double list [ ] = [ ];
Type?
W
6-1
6. Lists
[ Reade, Ch. 3 ]
Informally: for all types T , there is a list of objects of type T :
[a1 , . . . , an ]
(n 0).
Not random access, but access from the left end (like a stack, with top
at left).
Formally:
Lists are formed with two constructors
5-1
5. Lazy and Eager Evaluation
[ Reade Chs. 8, 9 ]
Order of evaluation of an expression usually makes no dierence; e.g.:
Example 1.
let val x = 5 + (4 2)
in (4 x) + (3 - x)
end;
If evaluate x before substitution: eager or strict.
If evaluate expr. after
12-1
12. Two Paradigms of Programming Languages:
Imperative and Functional
Interpreting an Imperative Language in SML
An imperative programming language (Pascal, C, Java, . . . ) has, as
its basic command, the assignment X := E.
A program in such a langua