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 Mutation

3 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

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

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

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

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

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

9 Streams

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

11 (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 ________________________ ________________________)))

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

13 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’

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

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

16 MC-eval

17 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

18 ;;; 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

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

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

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

22 (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))))

23 Environment Model

24 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

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

26 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:


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

Similar presentations


Ads by Google