Slide 2

Scheme] 
(+ 1 1)

2

Scheme] 
(begin
  (+ 1 1)
  (+ 1 3))

4

Scheme] 
(and (begin
       (debug-message "std" "branch 1\n")
       #t)
     (begin
       (debug-message "std" "branch 2\n")
       #t))

#t

Scheme] 
(or (begin
       (debug-message "std" "branch 1\n")
       #t)
     (begin
       (debug-message "std" "branch 2\n")
       #f))

#t

Scheme]

Slide 3

Scheme] 
(define result 0)

0

Scheme]
(for (x (list 1 2 3 4))
  (set! result (+ result x)))
Scheme] 
result

10

Scheme] 
(define (range n)
  (if (<= n 0)
      (list )
      (append (range (- n 1)) (list n))))

range

Scheme] 
(range 5)

(1 2 3 4 5)

Scheme] 
(define (sigma1 n)
  (define result1 0)
  (for (i (range n))
    (set! result1 (+ result1 i)))
  result1)

sigma1

Scheme] 
(sigma1 100)

5050

Scheme]
Scheme] 
(define (sigma2 n)
  (if (= n 0)
      0
      (+ n (sigma2 (- n 1))))); tail recursive

sigma2

Scheme] 
(sigma2 100)

5050

Scheme]

Slide 4

Scheme] 
(define (sum start end)
  (cond ((> start end) 0)
        ((= start end) end)
        (else (+ start
                 (sum (+ start 1) end)))))

sum

Scheme] 
(sum 2 100)

5049

Scheme]

Slide 5

Scheme] 
(define (fib n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        (else (+ (fib (- n 1))
                 (fib (- n 2))))))

fib

Scheme] 
(fib 2)

1

Scheme] 
(fib 4)

3

Scheme] 
(stree->tree
 '(tree "fib 2"
        (tree "fib 1" (tree 1))
        (tree "fib 0" (tree 0))))

Scheme] ;
scheme tree ‘'

Slide 6

Scheme] 
(define (fib2 n)
  (define a 0)
  (define b 1)
  (define old_b 0)
  (for (i (range n))
    (set! old_b b)
    (set! b (+ a b))
    (set! a old_b))
  a)

fib2

Scheme] 
(fib2 2)

1

Scheme]
Scheme] 
(define (fib3 n)
  (define (fib_ab n a b)
    (if (<= n 0)
        a
        (fib_ab (- n 1) b (+ a b))))
  (fib_ab n 0 1))

fib3

Scheme] 
(fib3 -1)

0

Scheme]