150 likes | 296 Views
CS116 – Tutorial 3 . Accumulative Recursion/Runtime. Reminders: . Assignment 3 is due on Tuesday, February 4th at 11:00am. s um-list (structural-recursion). ;; sum- list: ( listof num ) -> num ; ; Purpose: ;; consumes a list of numbers and produces their sum . ; ; Examples :
E N D
CS116 – Tutorial 3 Accumulative Recursion/Runtime
Reminders: • Assignment 3 is due on Tuesday, February 4th at 11:00am
sum-list (structural-recursion) ;; sum-list: (listofnum) -> num ;; Purpose: ;; consumes a list of numbers and produces their sum. ;; Examples: ;; (sum-list empty) => 0 ;; (sum-list '(1 2 3)) => 6 ;; Body: (define (sum-list alist) (cond [(empty? alist) 0] [else (+ (first alist) (sum-list1 (rest alist)))])) ;; Tests: (check-expect (sum-list1 empty) 0) (check-expect (sum-list1 '(1)) 1) (check-expect (sum-list1 '(1 2 3 4 5)) 15)
Trace of sum-list (structural recursion) (sum-list (list 1 2 3)) • (+ 1(sum-list (list 2 3))) • (+1(+2(sum-list (list 3)))) • (+1(+2(+3(sum-list empty)))) • (+1(+2(+3 0))) • (+1(+2 3)) • (+1 5) • 6
Question 1 • Write an accumulatively recursive function sum-list which consumes a list of numbers and produces their sum. • Trace(sum-list (list 1 2 3)) • Compare this accumulative version with the structural recursive version from earlier.
Trace of sum-list (accumulative recursion) • (sum-list (list 1 2 3)) • (sum-list-acc (list 1 2 3) 0) • (sum-list-acc (list 2 3) (+ 0 1)) • (sum-list-acc (list 2 3) 1) • (sum-list-acc (list 3) (+ 1 2)) • (sum-list-acc (list 3) 3) • (sum-list-acc empty (+ 3 3)) • (sum-list-acc empty 6) • 6
Question 2 • Develop an accumulatively recursive function list-to-num that consumes a list, digits, of numbers between 0 and 9, and returns the number corresponding to digits. • For example, • (list-to-num '(9 0 8)) => 908 • (list-to-num '(8 6)) => 86 • Trace (list-to-num '(8 0 2))
Trace of list-to-num • (list-to-num ‘(8 0 2)) • (list-to-num-acc ‘(8 0 2) 0) • (list-to-num-acc‘(0 2) (+ (* 10 0) 8)) • (list-to-num-acc‘(0 2) 8) • (list-to-num-acc‘(2) (+ (* 10 8) 0)) • (list-to-num-acc‘(2) 80) • (list-to-num-accempty (+ (* 10 80) 2)) • (list-to-num-accempty 802) • 802
Question 3 • Write an accumulatively recursive function find that reads in a list of symbols alos and a symbol sym, and returns the list of indices of positions in alos with symbol sym. Recall that the first position in a list has index 0. You may use reverse. • For example, (find '(a v d v) 'v) should return '(1 3), while (find '(a v d v) 'q) should return empty.
Question 4 • Write an accumulatively recursive function count-max that reads in a nonempty list of numbers alon and returns the number of times the largest number in alon appears. • For example, • (count-max (list 1 3 5 4 2 3 3 3 5)) => 2 • since the largest element of the list, 5 appears twice. Your function should pass through the list only once.
Question 5 • For each of the following determine what the worst-case runtime is.
Determine the worst-case run-time in terms of n, assuming n elements in lon (define (sum-list1 lon) (cond [(empty? lon) 0] [else (+ (first lon) (sum-list1 (rest lon)))]))
Determine worst-case run-time in terms of n, assuming n elements in loi (define (evens loi) (filter even? loi))
Determine the worst-case run-time in terms of n (define (create-number-lists n) (cond [(= n 0) empty] [else (cons (build-list n identity) (create-number-lists (sub1 n)))]))
Determine the worst-case run-time in terms of n (define (create-a-list n) (cond [(even? n) empty] [else (build-list 3 (lambda (y) (+ n y)))]))