You are on page 1of 2

CS 61A Scheme Midterm 2 Cheat Sheet. ((nlll?

mlist) '())
;; Comments on EVAL-1 of Scheme-1 (else (cons (m-car mlist)
;; There are four basic expression types in Scheme: (m-list-to-reg-list (m-cdr mlist))))))
1. self-evaluating (a/k/a constant) expressions: numbers, #t, etc. TREE
2. symbols (variables) ; Constructor for Trees:
3. special forms (in this evaluator, just QUOTE, IF, and LAMBDA) ; (make-tree datum children)
4. procedure calls (can call a primitive or a LAMBDA-generated ; datum can be anything
procedure) ; children is a LIST of trees
AND-EXP [Lab 9] ; we call a list of trees a "forest"
((AND-EXP? EXP) (EVAL-AND (CDR EXP))) ;; added ; Selectors for Trees:
(define (eval-and subexps) ; (datum node)
(if (null? subexps) ; returns the datum of a node
#T ; Trivial case: (AND), returns #T ; (children node)
(let ((result (eval-1 (car subexps)))); else eval first one. ; returns the children of a node
(cond ((null? (cdr subexps)) result); Last one, return its value. (define (make-tree datum children) (list children datum))
((equal? result #F) #F); False, end early. (define datum cadr)
(else (eval-and (cdr subexps))))))) ; else do the next one. (define children car)
DEEP LIST (define make-tree cons)
Deep List Map. Map version (define datum car)
(define (deep-map fn DL) (define children cdr)
(map (lambda (x) (if (list? x) Max with Mutual Recursion, not tested
(deep-map fn x) (define (max-children tree)
(fn x))) DL)) (max (length (children tree))
Deep List Map. Cons Cars version (max-children-forest (children tree)) )
(define (deep-map fn DL) (define (max-children-forest f)
(cond ((null? DL) „()) (if (null? f)
((list? (car DL)) 0
(cons (deep-map fn (car DL)) (max (max-children (car tree)
(deep-map fn (cdr DL))) (max-children-forest (cdr f))) )
(else (cons (fn (car DL)) Map-tree
(deep-map fn (cdr DL)))))) (define (treemap fn tree)
Same-Sublist, not tested (make-tree (fn (datum tree))
(define (same-sublist? a b DL) (map (lambda (t) (treemap fn t))
(cond ((null? DL) #f) (children tree))))
((and (member a DL) Treeify, List -> Tree [1997 Midterm 2]
(member b DL)) #t) ((3 + 4) * (7 - (2 / 2)))
);case 1 (define (treeify computation)
((list? (car DL)) (or (same-sublist? a b (car DL)) (if (number? computation)
(same-sublist? a b (cdr DL)) (make-tree computation '())
);or (make-tree (cadr computation)
);case 2 (list (treeify (car computation))
(else (same-sublist? a b (cdr DL)) (treeify (caddr computation))))))
);else, the empty list tree-member, Final Review
);cond (define (tree-member? x tree)
);define (if (eq? x (datum tree))
Deep-Accumulate [1998 Fall Midterm 2] #t
(define (deep-accumulate op init struct) (forest-member? (children tree))))
(cond ((null? struct) init) (define (forest-member? x forest)
((not (pair? struct)) struct) (cond ((null? forest) #f)
(else (op (deep-accumulate op init (car struct)) ((tree-member? x (car forest)) #t)
(deep-accumulate op init (cdr struct)))))) (else
Deep List -> Flat List, not tested (forest-member (cdr forest)))))
(define (flatten ls) Data-Directed Programming
(cond (get „foo „bar) --> #f
((null? '()) > (put „foo „bar „hello) --> okay
((list? (car ls)) > (get „foo „bar) --> hello
(append (flatten (car ls)) Tagged Data [2.4.2]
(flatten (cdr ls))) (define (attach-tag type-tag contents)
(else (cons type-tag contents))
(cons (car ls) (flatten (cdr ls)))))) (define (type-tag datum)
(define (m-car p) (if (pair? datum)
(p 'car)) (car datum)
(define (m-cdr p) (p 'cdr)) (error "Bad tagged datum -- TYPE-TAG" datum)))
(define (m-cons x y) (define (contents datum)
(lambda (msg) (if (pair? datum)
(cond (cdr datum)
((eq? msg 'car) x) (error "Bad tagged datum -- CONTENTS" datum)))
((eq? msg 'cdr) y) Typed Multiplier
(else "Error: Unknown msg")))) (define (times a b)
(define (m-list-to-reg-list mlist) (attach-tag
(cond (cond ((equal? (type-tag a) (type-tag b))
(word 'sq- (type-tag a)))
((GET (TYPE-TAG A) (TYPE-TAG B))) (empty-list? bst)
((GET (TYPE-TAG B) (TYPE-TAG A))) (height? bst) -> max depth
(else (word (type-tag a) '- (type-tag b)))) ((symbol? ...
(* (contents a) (contents b)))) ((if? ...
((quote?...
Message Passing ((lambda?...
(define (make-circle rad) ((pair? exp)
(lambda (msg) (apply-1 (eval-1 (car exp))
(cond ((equal? msg „area) (map eval-1 (cdr exp))))
(* pi rad rad)) BST MAX, not tested
((equal? msg „perimeter) (define (max bst)
(* 2 pi rad)) (if (empty-list? (right-child bst))
(else (error “bad message”))))) (datum bst)
Object-Oriented Programming (OOP)____ (max (right-child bst))
Smart Lock ;if
(define-class (lock my-pin) );define
(instance-vars (open? #f)) BST WIDTH, not tested
(METHOD (CORRECT-PIN? PIN) (define (width bst)
(EQUAL? PIN MY-PIN)) (if (empty-bst? bst)
(method (open pin) 0
(cond ((not (ASK SELF 'CORRECT-PIN? PIN)) (max (+ 2 (height (left-child bst))
'(sorry wrong pin)) (height (right-child bst)))
(open? (width (left-child bst))
'(lock is open already!)) (width (right-child bst))) ))
(else
general coding locate, 1998 final
(set! open? #t)
(define (locate value struct)
'(lock opens))))
(define (locate1)
(method (close)
(define (help struct fn)
(cond ((not open?)
(cond ((equal? value struct) fn)
'(lock is closed already!))
(else (require (pair? struct))
(else
(let ((cxr (amb car cdr)))
(set! open? #f)
(help (cxr struct) (compose cxr
'(lock closes)))))
fn))))))
(define-class (smart-lock my-pin)
(help struct (lambda (x) x)))
(parent (lock my-pin))
(amb (locate1) #f))
(instance-vars (errors 0))
(method (open pin) Other maybe useful, untested codes
(cond ((>= errors 3) ;;SAME-SUBLIST? , not working yet
'(lock shut down)) =========================
((not (ASK SELF 'CORRECT-PIN? PIN)) (define (same-sublist? a b DL)
(set! errors (+ errors 1)) (cond ((null? DL) #f)
(usual 'open pin)) ((and (member a DL)
(else (member b DL)) #t)
(usual 'open pin))))) );case 1
Length ((list? (car DL)) (or (same-sublist? a b (car DL))
(define-class (pair the-car the-cdr) (same-sublist? a b (cdr DL))
(method (length) );or
(+ 1 (ask the-cdr 'length)))) );case 2
(define-class (empty-list) (else (same-sublist? a b (cdr DL))
(method (length) );else, the empty list
0)) );cond
(define (new-cons a b) (instantiate pair a b)); This doesn't change );define
(define (new-cdr p) (ask p 'the-cdr)); This doesn't change
;=====TABLE ,NOT CHECKED ======
(define new-nil (instantiate empty-list)); NEW!
(define-class (table)
(define my-list (new-cons 3 new-nil)) ; CHANGED!
(instance-vars (table-entries '()))
(define other-list (new-cdr my-list)) ; This doesn't change
(method (put var val)
Now we get the correct length both for pairs and for empty lists:
(set! table-entries (cons (cons var val) table-entries)))
> (ask my-list 'length) -- 1
(method (helper var L)
> (ask other-list 'length )-- 0
(if (null? L)
ASSOC #f
(define (assoc key a-list) (if (equal? (caar L) var)
(cond ((NULL? a-list) #f) (cdar L)
((equal? key (ASSOCIATION-KEY (CAR a-list))) (ask self 'helper var (cdr L)))))
(CAR a-list)) (method (get var)
(else (assoc key (CDR a-list)))) ) (ask self 'helper var table-entries)))
;=====TABLE ,NOT CHECKED ======
BST
(make-bst
(datum
(left-child
(right-child
(leaf?

You might also like