;; QUICK IMPLEMENTATIONS FOR VISUALIZING ;; THE GROWTH OF FUNCTIONS AND THE CONSEQUENCES ;; OF IMPLEMENTATIONS WITH DIFFERENT ;; ASYMPTOTIC COMPLEXITY. ;; (two-to-the k) ;; ;; K is a natural number. ;; result is 2 raised to the Kth power. (define (two-to-the k) (if (= k 0) 1 (* 2 (two-to-the (- k 1))))) ;; (two-times l) ;; ;; Multiply each element in list L by 2. ;; L is a list of numbers ;; with generic ith element Ei. ;; result is a list ;; whose generic ith element is 2*Ei. (define (two-times l) (if (null? l) l (cons (* 2 (car l)) (two-times (cdr l))))) ;; Midterm definition of eval-poly, O(n) (define (eval-poly p x) (cond [(null? p) 0] [else (+ (car p) (* x (eval-poly (cdr p) x)))])) ;; (pow x n) ;; ;; X is a number; N a natural number. ;; Return x raised to the nth power. (define (pow x n) (if (= n 0) 1 (* x (pow x (- n 1))))) ;; Intuitive definition of eval-poly, O(n^2) ;; in two parts, using an auxiliary counter ;; variable and the pow function. (define (ep p x n) (if (null? p) 0 (+ (* (car p) (pow x n)) (ep (cdr p) x (+ n 1))))) (define (epx p x) (ep p x 0)) ;; Midterm definition of add-poly (define (add-poly p1 p2) (cond [(null? p1) p2] [(null? p2) p1] [else (cons (+ (car p1) (car p2)) (add-poly (cdr p1) (cdr p2)))])) ;; O(n^2) definition of diff-poly (define (diff-poly p) (cond [(null? p) '()] [else (add-poly (cdr p) (cons 0 (diff-poly (cdr p))))])) ;; O(n) definition of diff-poly, in two parts. (define (diff-aux p n) (cond [(null? p) '()] [else (cons (* n (car p)) (diff-aux (cdr p) (+ n 1)))])) (define (diff-poly2 p) (if (null? p) '() (diff-aux (cdr p) 1))) ;; (MLL N) ;; ;; Short for make-long-list. ;; N is a natural number. ;; Creates a list of length N ;; whose elements are random integers between 1 and 100. ;; ;; Useful for making big example instances for ;; diff-poly and eval-poly, so you can see ;; the consequences of asymptotic complexity. (define (mll n) (if (> n 0) (cons (random 100) (mll (- n 1))) '())) ;; (FIB N) ;; ;; naive O(2^n) implementation of the fibonacci function. ;; calculates fib n-1 and fib n-2 separately before adding ;; them together, so the computation path forks at each ;; step of recursion, creating a binary tree of depth n. (define (fib n) (if (< n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))) ;; (FIB-AUX left# fibi fibiplus1) ;; ;; O(n) implementation of the fibonacci function ;; which calculates fibonacci numbers forward ;; through the sequence up to a specified limit. ;; LEFT# is the number of fibonacci numbers ;; still to go before the specified computation ;; is done. When it's 0, we just return the last ;; fibonacci number we've made. ;; ;; Otherwise, the next fibonacci number is the ;; sum of the previous two. They've been passed ;; in as FIBI and FIBIPLUS1. To update the series ;; we discard FIBI and use FIBIPLUS1 and ;; FIBI + FIBIPLUS1. (define (fib-aux left# fibi fibiplus1) (if (= left# 0) fibiplus1 (fib-aux (- left# 1) fibiplus1 (+ fibi fibiplus1)))) ;; (FASTFIB N) ;; ;; Returns the Nth fibonacci number ;; (the same thing as FIB N) but ;; works by starting up the sequence ;; and letting fib-aux work forward ;; until the Nth number is reached. (define (fastfib n) (if (< n 1) 1 (fib-aux (- n 1) 1 1)))