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

Notes: Wednesday 4 February 2004
Schedule

Code
(define (dofor proc start end)
    (if (> start end)
        null
        (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)
        null
        (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)
      accum
      (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)
  (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

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

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