CS115 INTRODUCTION TO COMPUTER SCIENCE 1 Additional Notes Module 5
Example my-length (Slide 17) 2 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length empty) alos is replaced with empty [(empty? empty) 0] [else (+ 1 (my-length (rest empty)))]) [true 0] [else (+ 1 (my-length (rest empty)))]) from Module 3, cond substitution rules: [true exp] ) exp 0
Example my-length (Slide 17) 3 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) alos is replaced with (cons 'a (cons 'b empty))) [(empty? (cons 'a (cons 'b empty))) 0] [else (+ 1 (my-length (rest (cons 'a (cons 'b empty)))))]) [false 0] [else (+ 1 (my-length (rest (cons 'a (cons 'b empty)))))]) from Module 3, cond substitution rules: [false ][exp1 exp2] ) [exp1 exp2] )
Example my-length (Slide 17) 4 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) [else (+ 1 (my-length (rest (cons 'a (cons 'b empty)))))]) from Module 3, cond substitution rules: [else exp]) exp (+ 1 (my-length (rest (cons 'a (cons 'b empty))))) (+ 1 (my-length (cons 'b empty))) we need to function my-length again
Example my-length (Slide 17) 5 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) (+ 1 alos is replaced with (cons 'b empty)) [(empty? (cons 'b empty)) 0] [else (+ 1 (my-length (rest (cons 'b empty))))])) (+ 1 [false 0] [else (+ 1 (my-length (rest (cons 'b empty))))])) (+ 1 [else (+ 1 (my-length (rest (cons 'b empty))))]))
Example my-length (Slide 17) 6 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) (+ 1 (+ 1 (my-length (rest (cons 'b empty))))) we need to function my-length again (+ 1 (+ 1 (my-length empty ))) (+ 1 (+ 1 [(empty? empty) 0] [else (+ 1 (my-length (rest empty)))])))
Example my-length (Slide 17) 7 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length (cons 'a (cons 'b empty))) (+ 1 (+ 1 [true 0] [else (+ 1 (my-length (rest empty)))]))) (+ 1 (+ 1 0)) (+ 1 1) 2
Example: Factorial 8 f ( n) n f 1 ( n 1) if n else 0 base case f(2) = 2 x f(1) f(1) = 1 x f(0) f(0) = 1 Finally, to solve f(2), we now have: f(2) = 2 x 1 x 1 f(2) = 2
Example: Factorial 9 Visualizing Recursion using Factorial Example Example recursion trace: return 4 * 6 = 24 final answer recursivefactorial ( 4 ) return 3 * 2 = 6 recursivefactorial ( 3 ) return 2 * 1 = 2 recursivefactorial ( 2 ) return 1 * 1 = 1 recursivefactorial ( 1 ) return 1 recursivefactorial ( 0 ) Michael T. Goodrich, Roberto Tamassia, Michael H. Goldwasser, Data Structures and Algorithms in Java 6/e
Example: Factorial 10 In DrRacket: (define (f n) [(equal? n 0) 1] [else (* n (f (- n 1)))])) recursion base case f ( n) n f 1 ( n 1) if n else 0
Removing Elements from a List Slide 41 11 (define (removal n alon) [(empty? alon) empty] [else example: remove 5 5 4 5 9 first [(equal? (first alon) n) (removal n (rest alon))] [else (cons (first alon) (removal n (rest alon)))])])) is alon empty? Yes, return empty and exit removal function (base case) No, check the following cases: is n equal to first element in alon? Yes Call removal and pass the rest of alon No Create a new list with first element of alon and removal to check on the rest of the elements in alon
Example: Fibonacci 12 Fibonacci series: 0, 1, 1, 2, 3, 5, 8... Every number is the sum of the previous two f(0) = 0 f(1) = 1 base cases fib( n ) = fib( n - 1 ) + fib( n 2 ) Fibonacci in DrRacket (define (f n) [(or (equal? n 0) (equal? n 1)) n] [else (+ (f (- n 1)) (f (- n 2)))]))