image University of Washington Computer Science & Engineering
  CSE 341Sp '06:  Additional HW #5 Examples
  CSE Home   About Us    Search    Contact Info 

Here are the examples Jonah gave in section 5/11. There's one use of dysfun-stmt->scheme-expr, one use of dysfun-stmts->scheme-exprs, one use of dysfun-func->scheme-lambda, and a few of unbound-vars. He also cleaned up the indentation on the output so it's more readable than what DrScheme prints out. (This is just to help you read the examples; you do not need to do anything special about formating the code you generate.) Note that the while-loop function names are randomized, so they'll actually be different each time you run things.

;;;Example 0

> (define testcase '(5 / 2 * f(b) + g(a,b,c) * h(f(5 + 6,i())) + x(y(z())))) 
> (dysfun-expr->scheme-expr testcase)

=> (+ (* (/ 5 2) (f b)) (* (g a b c) (h (f (+ 5 6) (i)))) (x (y (z))))

;;; Example 1

> (define prog1 '(
    if (x + 1 < y)
    {
      y := y - 1 !
      x := x + 1 !
    }
  ))

> (unbound-vars '() prog1)
=> (y x)

> (dysfun-stmt->scheme-expr prog1)
=> (if (< (+ x 1) y)
      (begin
         (set! y (- y 1))
         (set! x (+ x 1))))


;;; Example 2

> (define prog2 '(
    n := 12 !
    c := 0 !
    while (n > 0)
    {
      c := c + n !
      n := n - 1 !
    } !
  ))

> (unbound-vars '() prog2)
=> (c n)

> (dysfun-stmts->scheme-exprs prog2)
=> ((set! n 12)
    (set! c 0)
    (letrec
      ((g49 (lambda ()
               (if (> n 0)
                  (begin
                    (set! c (+ c n))
                    (set! n (- n 1))
                    (g49))))))
      (g49)))


;;; Example 3

> (define prog3 '(
    func div (n, d) {
      r := n !
      q := 0 !
      if (d = 0)
      {
        return 0 !
      } !
      while (r >= d)
      {
        q := q + 1 !
        r := r - d !
      } !
      return q !
    }
  ))

> (unbound-vars '() prog3)
=> (div n r d q)

  ;;; above is correct, but following is more like how you will use it:
> (unbound-vars '(n d) (cdddr prog3))
=> (r q)

> (dysfun-func->scheme-lambda prog3)
=> (lambda (n d)
    (let/cc
      return
      (let ((r (void)) (q (void)))
        (set! r n)
        (set! q 0)
        (if (= d 0) (begin (return 0)))
        (letrec
           ((g44 (lambda ()
                      (if (>= r d)
                         (begin
                           (set! q (+ q 1))
                           (set! r (- r d))
                           (g44))))))
          (g44))
        (return q))))


CSE logo Computer Science & Engineering
University of Washington
Box 352350
Seattle, WA  98195-2350
(206) 543-1695 voice, (206) 543-2969 FAX
[comments to cse341-webmaster at cs.washington.edu]