Download Data Structures in Scheme

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Structures in Scheme

Building data structures: Java
class Circle {
private double center_x, center_y, radius;
Circle(double x, double y, double r) {
this.center_x = x;
this.center_y = y;
this.radius = r;
}
};
Data Structures in Scheme
Building data structures: Scheme - use list in a
constructor function:

(define make-circle
(lambda (center-x center-y radius)
(list center-x center-y radius)))
Data Structures in Scheme
Accessing fields: use list-ref in a selector
function:

double getRadius () {
// Java
return radius;
}
(define get-radius
(lambda (circle)
(list-ref circle 2)))
; Scheme
Local declaration/scope via let
double area() {
double pi = 3.14159, r = radius;
return (pi * r * r);
}
(define area
(lambda (circle)
(let ((pi 3.14159)
(r (get-radius circle)))
(* pi r r))))
Local declaration/scope via let
(let ( (var1 val1)
(var2 val2)
..
(varN valN) )
result )
Shortcut with letrec
> (letrec ((a 3)
(b (* 2 a))
(c (+ b 1)))
c)
7
> (let ((a 3)) ; long version

(let ((b (* 2 a)))

(let ((c (+ b 1)))

c)))
7

Sequential execution via begin
(define (prompt-for-command-char prompt)
(begin
(write prompt)
(read)))
The Main Course: Essentials of
Programming Languages
The Big Idea: The interpreter for a computer
language is just another program.


Think of quotes: The claim “Nerds are cool” is
very true.

This simple fact has profound implications.

Brings us to the Halting Problem (Turing), and
related diagonalization proofs (Cantor, Gödel)
The Halting Problem

Formulated by Hilbert as the Entscheidungsproblem
(lit., “Decision Problem”, 1928)

Is there a general method that will tell us whether or
not a given statement is true?

Gödel: NO! (For arithmetic at least, 1930's)

“Cocktail party” proof:
There is no proof for this statement.
The Halting Problem

Is there a general program that will tell us whether or
not a given program halts on a given input?

Turing: NO! (1937)
“Cocktail party” proof:
The Halting Problem
(à la Abelson & Sussman)
(define run-forever
(lambda () (run-forever)))
(define invert
function
(lambda (program)
(if (halts? program program)
(run-forever)
input
'halted)))
Can we write the (general) halts? predicate?
The Halting Problem
(define halts?
(lambda (program input) ... )
(define invert
(lambda (program)
(if (halts? program program)
(run-forever)
'halted)))
(invert invert) ; uh-oh!
The Halting Problem
If invert halts on itself, then it runs forever. If it
runs forever, then it halts: CONTRADICTION

This means that our assumption (the existence
of halts?) must have been wrong, Q.E.D.

Reductio ad absurdum by diagonalization:
Diagonalization #1: Halting Problem
input length
program
length
exit
37
exit ...
9
...
(exits)
(exits) . . .
(loops)
(loops) . . .
invert
52
(exits)
...
invert
?????
Diagonalization #2: Cantor's Proof
Can the real numbers be counted (matched 1:1
with the positive integers?)


Cantor (~1897): NO!

Proof: If reals (-∞ ... +∞) can be counted, then
certainly reals in (0 ... 1) can be counted. So let's
try:
Diagonalization: Cantor's Proof
1
2
3
.
.
.
.1082984911293...
.8304302821712...
.0024826578924...
So, now we've enumerated every real in (0,1), right?
Diagonalization: Cantor's Proof
Lets invert every number on the diagonal, by
subtracting it from 9:
1
2
3
.8082984911293...
.8604302821712...
.0074826578924...
.
.
.
Now look at the number on the diagonal: .867...
This diagonal number can't be in the first row,
because it differs in the first digit from the number in
that row.

It can't be in the second row, because it differs in the
second digit.

It can't be in the Nth row, because it differs in the
Nth digit.


Therefore it can't be in the table.

Therefore we haven't enumerated the reals in (0,1).

Therefore we can't enumerate the reals, Q.E.D.
Diagonalization #3: Do Mathematicians Work
Harder than Computer Scientists?
Are there some functions that cannot be computed – ie.,
for which we cannot write a computer program?

Consider a program to be a finite sequence of bits
(certainly valid!), so we can count programs.

Consider just the class of functions that input a natural
number and output true or false (e.g., test for even, prime,
perfect)

Assume there is a program that computes each such
function….

input
0 1 2 3 4 5 6 7 8 9 …
program
even?
T F T F T F T F T F …
prime?
F F T T F T F T F F …
perfect?
T F F F F F T F F F …
p-o-2?
F T T F T F F F T F …
Invert diagonal:
input
0 1 2 3 4 5 6 7 8 9 …
program
even?
F F T F T F T F T F …
prime?
F T T T F T F T F F …
perfect?
T F T F F F T F F F …
p-o-2?
F T T T T F F F T F …

The diagonal is now


A valid function (assigns a unique value to each number)

Not in any row of the table
So there is at least one function that cannot be
computed!
Diagonalization: What to Do?

Go insane! (Cantor, Gödel, Turing)

Use type systems (Russell, Wittgenstein, Church, Curry)
e.g., square is undefined (type error) on strings.

Denial: Die ganzen Zahlen hat der liebe Gott gemacht,
alles andere ist Menschenwerk (Kronecker 1886)

Surrealism / Postmodernism:
Related documents