week4a_sol - CS61A Notes Week 7: Object oriented...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS61A Notes – Week 7: Object oriented programming (solutions) Taste the Rainbow (or: Dinner is not Ready) (define-class (bag) (instance-vars (skittles ‘())) (method (tag-line) ‘taste-the-rainbow) (method (add s) (set! skittles (cons s skittles))) (method (take) (let ((first (car skittles))) (set! skittles (cdr skittles)) first)) (method (take-color color) (let ((found (find (lambda(s) (eq? color (ask s ‘color))) skittles))) (set! skittles (remove found skittles)) found))) Directories and Files (Again) (define-class (file name content) (method (type) 'file) (method (size) (length content))) (define-class (directory name) (instance-vars (content '())) (method (type) 'directory) (method (add thing) (set! content (cons thing content))) (method (mkdir dir) (ask self 'add (instantiate directory dir))) (method (cd dir) (find (lambda(f) (eq? dir (ask f 'name))) content)) (method (mv thing dir) (let ((found (find (lambda(f) (eq? thing (ask f 'name))) content))) (ask (ask self 'cd dir) 'add found)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

week4a_sol - CS61A Notes Week 7: Object oriented...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online