;; The first three lines of this file were inserted by DrScheme. They record metadata
;; about the language level of this file in a form that our tools can easily process.
#reader(lib "htdp-beginner-reader.ss" "lang")((modname slides-3-fib) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ())))
; takes a positive integer and returns the associated fibnonacci number
; this function generates a tree-recursive process
(define (fib n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        (else (+ (fib (- n 1))
                 (fib (- n 2))))))

(check-expect (fib 4) 3)

; takes a positive integer and returns the associated fibonacci number
; this function calls another function that generates an iterative process
(define (fib2 n) (ifib 1 0 n))

; helper function called from fib2
; creates an iterative process to generate the fibonacci number
; counts up from 0 generating fibonacci numbers for the n that
; you want
(define (ifib next-fib cur-fib cnt)
  (if (= cnt 0)
      cur-fib
      (ifib (+ next-fib cur-fib)
            next-fib
            (- cnt 1))))

(check-expect (fib2 4) 3)

