Download presentation
Presentation is loading. Please wait.
1
Types of the past exam questions I. Write/Complete Code II. Evaluate Expressions III. Analyze Complexity IV. Meta-circular Evaluator
2
I.Write Code: Tools of the trade Facts about Scheme ◦ Functions as data (e.g. pass as parameter) ◦ Special forms (e.g. lambda, define, if, and) ◦ Data Types (e.g. symbol, pair, list, streams) ◦ Reference and Comparison (e.g. eq?, equal?) ◦ Helpful functions (map, filter etc.) Methods for building functions ◦ Iteration/Recursion (nested helper function) ◦ Abstraction barriers (constructor/selector…) ◦ Bottom-up/Top-down
3
I.Evaluate Expression: Tools of the Trade Substitution-model ◦ Substitute expressions for names Environment ◦ Connection between names and expressions ◦ Define rule ◦ Set rule
4
Application Rule (for P) 1. Create a new frame A 2. Make A into an environment E 3. In A, bind parameters of P to the argument values 4. Evaluate the body of P - E as the current environment
5
III.Analyze Complexity: Tools of the trade Choose input parameter(s) What operations will count? Write recurrence ◦ T(a,b)=c*T(a/2n,b/2)+d*F(a/2,b/2) Method A: Guess solution and prove by induction Method B: Open few stages, find a rule, and calculate sum
6
IV.Meta- Circular Evaluator: Tools of the Trade Apply-Eval loop Eval: evaluate expression in a given environment Apply : apply procedure to the arguments Adding new constructs: ◦ Add case in “eval” to recognize new construct ◦ Translate to other or evaluate directly
7
Streams Process infinite stream of lists to produce infinite stream of pairs ◦ Input: [ (0 1 2 3 0 5) (0 2) (1 0 2)..] ◦ Output: [(0.1) (0.2) (0.3) (0.5) (1.1) (2.0) (2.2)…] Each pair is a list number (starting from zero) and a position of non-zero element within the list Assume each list has at least one non-zero element
8
Streams: Solution Strategy Top-bottom: decompose into smaller problems then solve them (define (process-stream strm num-lst) (stream-append (process-list (stream-car strm) num-lst 0) (process-stream (stream-cdr strm) (+ num-lst 1)) )) 2 sub-problems: process-list and stream-append
9
Streams: Process-list (define (process-list lst lst-pos elem-pos) (cond ((null? lst) null) ((= (car lst) 0) (process-list (cdr lst) lst-pos (+ elem-pos 1))) (else (stream-cons (cons lst-pos elem-pos) (process-list (cdr lst) lst-pos (+ elem-pos 1)))) ))
10
Streams: Bottom-Up Easy to preprocess into stream of triples ◦ (List-num pos value) Step II: Filter-out zeros (stream-filter)
11
Streams: Bottom-Up (define (stream->triples strm list-num pos-num) (let ((first-lst (stream-car strm))) (if ((null? first-lst) (stream->triples (stream-cdr strm)(+ list-num 1) 0)) (stream-cons (list list-num pos-num (car first)) (stream->triples (stream-cons (cdr first) (stream-cdr stream) ) list-num (+ pos-num 1) ) )))
12
Lists A tree defined as (2 ((3 7) 8) ((4 5))) A: Reverse the order of the (direct) sub-trees iteratively (((4 5)) ((3 7) 8) 2) B: Reverse the order of the sub-trees in the levels matching a given predicate.
13
Lists: A Iterative – pass the result as an argument. (define (reverse-sub-trees tr) (define (reverse-iter tr new-tr) (if (null? tr) new-tr (reverse-iter (cdr tr) (cons (car tr) new-tr)))) (reverse-iter tr '()))
14
Lists: B Need to process first sub-tree (car tr) and the other sub-trees (cdr tr), and append the resulting sub-trees – order depends om (pred level). (define (reverse-some-levels tr pred) (define (reverse-iter tr pred level) (cond ((null? tr) '()) ((not (pair? tr)) tr) ((pred level) (append (reverse-iter (cdr tr) pred level) (list (reverse-iter (car tr) pred (+ 1 level))))) (else (cons (reverse-iter (car tr) pred (+ 1 level)) (reverse-iter (cdr tr) pred level))))) (reverse-iter tr pred 1))
15
SOS-interleave - Graphically S 11 S 12 S 13... S 21 S 22 S 23... S 31 S 32 S 33...... 1 2 3
16
SOS-interleave - Code (define (interleave s1 s2) (if (stream-null? s1) s2 (cons-stream (stream-car s1) (interleave s2 (stream-cdr s1))))) (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
SOS-interleave - Test Create an integers SOS: (define (int_strm int) (cons-stream int (int_strm int))) (define (ints n) (cons-stream (int_strm n) (ints (+ n 1)))) > (define sos_ints (ints 1)) sos_ints: [(1 1 1 1 1 …) (2 2 2 2 2 …) (3 3 3 3 3 …) … ] > (display-stream-head (sos-interleave sos_ints) 10) 1 1 2 1 2 1 3 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 5 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1 6 1 2 1 3 1
18
Environments Model (define (make-sqrt x) (define (improve guess) (average guess (/ x guess))) (let ((guess 1.0)) (lambda() (set! guess (improve guess)) guess))) Assume that average is primitive
19
Environments Model (define 3-root-1 (make-sqrt 3)) (define 3-root-2 (make-sqrt (+ 2 1))) > (3-root-1) __________________ > (3-root-2) __________________ > (3-root-1) __________________ > guess reference to undefined identifier: guess
20
Environments Model > (3-root-1) 2 > (3-root-2) 2 > (3-root-1) 1.75 > guess error
21
Environments Model GE make-sqrt: 3-root-1:3-root-2: P: x B: (define (improve … E1 x: 3 Improve: P: guess B: (average…
22
MCE New object: (object ( ) … ( ))
23
MCE Example: (object point (v1 0) (v2 0) (v3 0) (set-point! (lambda(x y z) (set! v1 x)(set! v2 y)(set! v3 z) ‘ok)) (mirror! (lambda() (set! v1 (- v1)) (set! v2 (- v2)) (set! v3 (- v3)) ‘ok))
24
MCE >(point v1) 0 >((point set-point!) 4 3 3) ok >(point v1) 4 >((point mirror)) ok >(point v1) -4
25
MCE Predicate: object-def? Selectors: object-name, obkect-bindings’ binding-field, binding-exp Eval-object-def – creates a new environment that includes all fields. In the original env - object-name refers to: (define (make-object env) (list ‘object env)
26
MCE use for-each – receives a proc and a list and applies proc an each of the elements in the list (no returned value)
27
(define (eval-object-def exp env) (let ((object-env (extend-environment null null ________________))) (for-each (lambda(binding) (define-variable! __________________________________ __________________________________)) (object-bindings exp)) (define-variable! _________________________ _____________________________________ ____________________________________) ‘ok))
28
(define (eval-object-def exp env) (let ((object-env (extend-environment null null ____env_________))) (for-each (lambda(binding) (define-variable! (binding-field binding) (mc-eval (binding-exp binding) env) object-env)) (object-bindings exp)) (define-variable! (object-name exp) (make-object object-env) env) ‘ok))
29
MCE – cont’ Read a value of a field: (object-name field-name) (define (eval-object-access exp obj) (lookup-variable-value (cadr exp) (object-env object)))
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.