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

Notes: Wednesday 4 February 2004

(define (dofor proc start end)
    (if (> start end)
        (cons (proc start) 
                 (dofor proc (+ start 1) end))))

(define (intsto n) (dofor (lambda (x) x) 1 n))

(define (dountil proc stoptest incr current)
    (if (stoptest current)
        (cons (proc current) 
                 (dountil proc stoptest incr (incr current)))))

(define countdown
  (dountil (lambda (x) x) (lambda (val) (< val 1)) (lambda (x) (- x 1)) 10))

(define (for index stoptest combine next accum)
  (if (stoptest index)
      (for (next index) stoptest combine next (combine index accum))))

(define (gauss-sum n) 
  (for 1 (lambda (index) (<= index n)) + (lambda (x) (+ 1 x)) 0)) 

(define (fast-fibo n)
    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

Recursion is the root of computation since it trades description for time.
Alan Perlis
Using these Materials