University of Virginia, Department of Computer Science CS200: Computer Science, Spring 2004

Notes: Monday 2 February 2004

• Wednesday, 4 February: Read SICP, 2.1 and 2.2 (through page 127). You don't need to read the example section 2.2.4, but probably don't want to miss the footnote on page 127 about William Barton Rogers, MIT's founder, who left UVa after too many students were rioting outside his pavillion.
• Wednesday, 11 February: Problem Set 3
• Before 10 March: Read rest of GEB part I (Chapters 2-4 and 6-9, in addition to Chapters 1 and 5 you have already read). There will be no more specific reading assignments from GEB until end of Spring Break (there will be other reading assignments from SICP). By March 10th, you will be expected to have read all of Part I (through page 272) of GEB. I recommend reading about a Chapter a week, but if you prefer to read it all over Spring Break that is fine too.
Null/Nil Confusion

In SICP, they use nil. In MzScheme (DrScheme's language), they use null. The Revised5 Report on the Algorithmic Language Scheme mentions nil but uses the empty list (denoted by '()) instead. As the footnote on page 101 explains, the exact meaning and name for the empty list is cause for lots of dispute and confusion. We will use null in class to be consistent with MzScheme, and reserve nil for reporting soccer scores.

Code
```(define (gauss-sum n)
(if (= n 0) 0 (+ n (gauss-sum (- n 1)))))

(define (insertl lst f stopval)
(if (null? lst) stopval
(f (car lst) (insertl (cdr lst) f stopval))))

(define (intsfrom n)
(if (= n 0) null (cons n (intsfrom (- n 1)))))

(define (intsto n)
(if (= n 0) null (append (intsto (- n 1)) (list n))))

(define (gauss-sum n) (insertl (intsto n) + 0))
(define (factorial n) (insertl (intsto n) * 1))

(define (map f lst)
(if (null? lst) null (cons (f (car lst))  (map f (cdr lst))))

(define (map f lst)
(insertl lst (lambda (a b) (cons (f a) b)) null))
```
Challenge Problem

Many programming languages provide an interation construct known as a for loop or do loop. Your task today is to define a procedure for that can be used in Scheme for what for loops are for and do loops do in other languages.

Fortran (1954) provided DO loops like this:

```    SUM = 0
DO 100 I = 1, N
SUM = SUM + I
100 CONTINUE
```
This sums up the number from 1 to N. (The 100 after the DO is a label for the end of the loop, not how many times to iterate.)

Algol 60 (1960) introduced a for loop that is found in almost all common languages today (most of which evolved from Algol):

```sum := 0
for i := 1 until N do
sum := sum + i;
```
Your task is to define a procedure for that works like the Java for statement.

Your for procedure should have five parameters:

• index — the current index value
• test — a procedure that takes the current index value as input, and evaluates to #t if the loop should continue
• combine — a procedure that takes two parameters, the current accumulation and the current index value, and evaluates to the next accumulation value
• next — a procedure that takes the current index value as input, and evaluates to the next index value
• accum — the current accumulation value
The value of an application of for should be the final accumulation value.

You should also be able to use for to define gauss-sum like this:

```(define (gauss-sum n)
(for 1 (lambda (index) (<= index n)) + (lambda (x) (+ 1 x)) 0))
```
and fast-fibo as:
```(define (fast-fibo n)
(first
(for
1                       ; initial iteration value
(lambda (i) (<= i n))   ; test - if it evaluates to true when applied to current iteration value, keep going
(lambda (accum i)       ; combiner function
(list (+ (first accum) (second accum)) (first accum)))
(lambda (i) (+ i 1))    ; next function
(list 0 1))))           ; initial accumulator value
```
You should only need about four lines to define for. Here's one implementation: for.ss.

### Specifying For

Below is an excerpt from The Java Language Specification by James Gosling, Bill Joy, Guy Steele and Gilad Bracha that describes the syntax and evaluation rule for the for statement in Java. Recall the BNF shortcut of using square brackets like [ Article ] to indicate that Article is optional. I've changed their grammar slightly to be consistent with the BNF notation we have seen in class. Just skim this for now, but I want you to see what the language specification really looks like.

## 14.13 The `for` Statement

The `for` statement executes some initialization code, then executes an Expression, a Statement, and some update code repeatedly until the value of the Expression is `false`.

ForStatement ::= for ( [ ForInit ] ; [ Expression ] ; [ ForUpdate ] ) Statement
ForInit ::= StatementExpressionList
ForUpdate ::= StatementExpressionList
StatementExpressionList ::= StatementExpression
StatementExpressionList ::= StatementExpressionList ,  StatementExpression
The Expression must have type `boolean`, or a compile-time error occurs.

### 14.13.1 Initialization of `for` statement

A `for` statement is executed by first executing the ForInit code:

• If the ForInit code is a list of statement expressions (§14.8), the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If evaluation of any expression completes abruptly for some reason, the `for` statement completes abruptly for the same reason; any ForInit statement expressions to the right of the one that completed abruptly are not evaluated.
If the ForInit code is a local variable declaration, it is executed as if it were a local variable declaration statement (§14.4) appearing in a block. The scope of a local variable declared in the ForInit part of a `for` statement (§14.13) includes all of the following:

• Its own initializer
• Any further declarators to the right in the ForInit part of the `for` statement
• The Expression and ForUpdate parts of the `for` statement
• The contained Statement
If execution of the local variable declaration completes abruptly for any reason, the `for` statement completes abruptly for the same reason.

• If the ForInit part is not present, no action is taken.

### 14.13.2 Iteration of for statement

Next, a `for` iteration step is performed, as follows:

• If the Expression is present, it is evaluated, and if evaluation of the Expression completes abruptly, the `for` statement completes abruptly for the same reason. Otherwise, there is then a choice based on the presence or absence of the Expression and the resulting value if the Expression is present:
• If the Expression is not present, or it is present and the value resulting from its evaluation is `true`, then the contained Statement is executed. Then there is a choice:
• If execution of the Statement completes normally, then the following two steps are performed in sequence:
• First, if the ForUpdate part is present, the expressions are evaluated in sequence from left to right; their values, if any, are discarded. If evaluation of any expression completes abruptly for some reason, the `for` statement completes abruptly for the same reason; any ForUpdate statement expressions to the right of the one that completed abruptly are not evaluated. If the ForUpdate part is not present, no action is taken.
• Second, another `for` iteration step is performed.
• If execution of the Statement completes abruptly, see §14.13.3 below.
• If the Expression is present and the value resulting from its evaluation is `false`, no further action is taken and the `for` statement completes normally.
If the value of the Expression is `false` the first time it is evaluated, then the Statement is not executed.