1 / 15

CS116 – Tutorial 3

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 :

jerom
Download Presentation

CS116 – Tutorial 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS116 – Tutorial 3 Accumulative Recursion/Runtime

  2. Reminders: • Assignment 3 is due on Tuesday, February 4th at 11:00am

  3. 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)

  4. 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

  5. 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.

  6. 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

  7. 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))

  8. 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

  9. 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.

  10. 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.

  11. Question 5 • For each of the following determine what the worst-case runtime is.

  12. 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)))]))

  13. Determine worst-case run-time in terms of n, assuming n elements in loi (define (evens loi) (filter even? loi))

  14. 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)))]))

  15. 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)))]))

More Related