Presentation is loading. Please wait.

Presentation is loading. Please wait.

Extended Introduction to CS Preparation for Final Exam.

Similar presentations


Presentation on theme: "Extended Introduction to CS Preparation for Final Exam."— Presentation transcript:

1 Extended Introduction to CS Preparation for Final Exam

2 Complexity

3 Longest Monotone Sequence Given a sequence of numbers return the longest monotone sequence: > (lms (list 1 2 3 4)) (1 2 3 4) > (lms (list 1 3 2 4)) (1 3 4) > (lms (list 1 3 9 2 6 7 4 6 7)) (1 3 6 7 7)

4 Correct solution (define (lms sequence) (define (helper seq m) (if (null? seq) null (let ((current (car seq)) (rest (cdr seq))) (if (>= current m) (let ((l1 (helper rest m)) (l2 (cons current (helper rest current)))) (if (> (length l1) (length l2)) l1 l2)) ; choose the "best" sequence ; the current number is smaller than the maximum so far, ; and therefore cannot be included in the LMS (helper rest m))))) (define (minimum sequence) (accumulate min (car sequence) (cdr sequence))) (if (null? sequence) null (helper sequence (- (minimum sequence) 1)))) helper - will construct LMS given the a sequence and the current maximal value (m) Options: 1 - don't use the current number 2 - use the current number

5 Longest Monotone Sequence Time Complexity Assuming length is O(1): T(n) <= 2T(n-1) + c O(2^n)

6 Mutation

7 rotate! (define l ‘(1 2 3 4)) (rotate! l) l (4 1 2 3) First attempt: use set-cdr! Would it work? In which environment is l? and lst? 1 234 l lst

8 Correct solution (define (rotate! lst) (define (help p rest-lst) (cond ((null? rest-lst) (set-car! lst p)) (else (let ((temp (car rest-lst))) (set-car! rest-lst p) (help temp (cdr rest-lst)))))) (help (car lst) (cdr lst)) lst)

9 High-order functions Functions can receive other functions as input and may return functions as output

10 make-star g*(x) = number of times we need to apply g until g(g(g(…(g(x)…)))<=1

11 make-star (define (make-star g) (define (g* x) (if (<= x 1) 0 (+ 1 (g* (g x))))) g* )

12 log (log 4) ==> 2 (log 1) ==> 0 (log 5) ==> 3 (define log (make-star _________________ ) (lambda (x) (/ x 2))

13 Streams

14 SOS-interleave Receives a stream of streams Should return a stream of all members (in some order)

15 (define (sos-interleave sos) (let ((first-of-first (stream-car (stream-car sos))) (rest-of-first (stream-cdr (stream-car sos)) (rest (stream-cdr sos))) (cons-stream ________________________ ________________________)))

16 SOS-interleave (define (sos-interleave sos) (let ((first-of-first (stream-car (stream-car sos))) (rest-of-first (stream-cdr (stream-car sos)) (rest (stream-cdr sos))) (cons-stream first-of-first (interleave rest-of-first (sos-interleave rest)))))

17 Words List of symbols (‘a or ‘b) represent a word A stream of lists represents a language 1. Generate the language of all words containing only ‘a’ 2. Generate the language of all words starting with an ‘a’

18 All-a (define all-a (cons-stream '(a) (stream-map (lambda (x) (append x '(a))) all-a))))

19 Starting-with-a (define start-with-a (cons-stream '(a) (interleave (stream-map (lambda (x) (append x '(a))) start-with-a) (stream-map (lambda (x) (append x '(b))) start-with-a))))

20 MC-eval

21 Add a new special form – decrease (decrease proc num) If proc is compound, change its body so that it will return a number smaller by num than what it should have If proc is not compound return error Otherwise assume it returns a number

22 ;;; M-Eval input: (define (double x) (+ x x)) ;;; M-Eval value: ok ;;; M-Eval input: (double 3) ;;; M-Eval value: 6 ;;; M-Eval input: (decrease double 1) ;;; M-Eval value: ok ;;; M-Eval input: (double 3) ;;; M-Eval value: 5 ;;; M-Eval input (decrease + 1) Error: can apply decrase only to compound procedures

23 Section A (define (decrease? exp) ___________________________ ) (define (decrease-proc-name exp) ______________________ ) (define (decrease-number exp) _______________________ )

24 (define (decrease? exp) ___(tagged-list? exp 'decrease)___________________ ) (define (decrease-proc-name exp) ____(cadr exp)__________________________ ) (define (decrease-number exp) _____(caddr exp)_______________________ )

25 Section B Decrease returns the new procedure body (define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list)) (define (eval-decrease exp env) (let* ((proc-name ________________________________ ) (number ________________________________ ) (proc (_________________________ proc-name env))) (cond ( ____________________________________ (error “Can apply decrease only to compound procedures”)) (else (modify-body proc (decrease proc number)) ‘ok))))

26 (define (modify-body proc expressions-list) (set-car! (cddr proc) expressions-list)) (define (eval-decrease exp env) (let* ((proc-name ____(decrease-proc-name exp)________ ) (number ____(decrease-number exp)_____________ ) (proc (_____mc-eval _____________ proc-name env))) (cond ( _____(not (compound-procedure? proc))____________ (error “Can apply decrease only to compound procedures”)) (else (modify-body proc (decrease proc number)) ‘ok))))

27 (define (decrease proc number) ___ ___ ________________ ) Decrease returns the new procedure body

28 (define (decrease proc number) ___(list (list '- (cons 'begin (caddr proc)) number))___________________ ) Decrease returns the new procedure body

29 Environment Model

30 Functional tables (define (make-table) (lambda (x) (error "key not found:" x))) (define (find key table) (table key)) (define (insert key value table) (lambda (x) (if (equal? x key) value (find x table)))) Q2

31 Example (define t0 (make-table)) (define t1 (insert 'a 1 t0)) (define t2 (insert 'b 2 t1)) (find 'a t2) => 1

32 global env Pars: – Body: (lambda (x) (error...)) make-table: Pars: key, table Body: (table key) find: Pars: key, table, value Body: (lambda (x) (if...)) insert: E1 : Pars: x Body: (error...)) t0: Pars: x Body: (if...)) key: a value: 1 table: E2: t1: key: a table: E4: key: a table: E6: x: a E5: x: a E7: key: b value: 2 table: E3: Pars: x Body: (if...)) t2:

33 Delete key from table (define (delete key table) (lambda (x) (if (equal? x key) (error "key not found:" x) (find x table))))

34 Example (define t0 (make-table)) (define t1 (insert 'a 1 t0)) (define t2 (delete 'a t1)) (find 'a t2) => key not found: a

35 global env Pars: – Body: (lambda (x) (error...)) make-table: Pars: key, table Body: (table key) find: Pars: key, table, value Body: (lambda (x) (if...)) insert: E1 : Pars: x Body: (error...)) t0: Pars: x Body: (if...)) key: a value: 1 table: E2: t1: key: a table: E4: x: a E5: key: a table: E3: Pars: x Body: (if...)) t2:

36 Worst case running time of “find” The worst-case running time of find on a given table is proportional to the number of insert and delete operations performed to generate that table. When given a value that never appeared in any call to insert or delete, the search will invoke find on all the tables generated in these calls, up to the one generated by make-table, which will generate an error.


Download ppt "Extended Introduction to CS Preparation for Final Exam."

Similar presentations


Ads by Google