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

Notes: Monday 2 February 2004
Schedule

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:

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 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:

If execution of the local variable declaration completes abruptly for any reason, the for statement completes abruptly for the same reason.

14.13.2 Iteration of for statement

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

If the value of the Expression is false the first time it is evaluated, then the Statement is not executed.

cs200-staff@cs.virginia.edu
Using these Materials