{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

day15 - CS1 Introduction to Computation Day 15 More...

Info icon This preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 CS1: Introduction to Computation Day 15: November 17, 2008 More Environments & OOP
Image of page 1

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 Monty Python and the Holy Grail (1975)
Image of page 2
Caltech CS1 -- Fall 2008 Last week set! – allows us to change existing bindings – breaks our substitution model – no longer purely functional • environments – new way to model program execution – (necessarily) more complicated! – still "only a model"
Image of page 3

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 Today Design a message-passing object implementation Observe environments it creates Observe object-oriented programming (OOP) techniques along the way
Image of page 4
Caltech CS1 -- Fall 2008 cs1man (version 0.0)
Image of page 5

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 Defining an Interface What kinds of operations do we want to provide? What are the inputs and outputs to those operations?
Image of page 6
Caltech CS1 -- Fall 2008 cs1man ops • Constructor – makes a new, empty cs1man object – takes no arguments (define cs1-2008 (make-cs1man))
Image of page 7

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 cs1man operations add-student! <name> – adds a student to the system – takes single symbol as “name” – mutates internal data representation • get-students – accessor: returns list of all student names is-student? <name> – predicate – expects one argument, a name – returns #t if that name is in the system
Image of page 8
Caltech CS1 -- Fall 2008 Implementing This is a good start Implement this first; come back and add more features later!
Image of page 9

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 Implementing (define (make-cs1man-v0.0) (let ((students (list))) ;; a list of valid accounts ;; internal helper predicate to determine whether a ;; a student exists (define (is-student? name) (list? ( memq name students))) ;; the anonymous (unnamed) message-processing ;; lambda (lambda (op . args) ...)))
Image of page 10
Caltech CS1 -- Fall 2008 memq ( memq <value> <list>) • Returns #f if <value> not in <list> • If <value> in list, returns part of list starting from that value Can use as a true value anything not #f can act as a true value ( memq 10 '(1 2 3 4 5)) ;; #f ( memq 3 '(1 2 3 4 5)) ;; (3 4 5)
Image of page 11

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 list? • list? is just a predicate that returns #t if its argument is a list, and #f otherwise (list? 10) #f (list? (cons 1 2)) #f (list? '(1 2 3 4 5)) #t
Image of page 12
Caltech CS1 -- Fall 2008 list? (list? (memq val lst)) • returns #t if val is in lst , otherwise #f • Back to our example...
Image of page 13

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 (define (make-cs1man-v0.0) ( let ((students (list))) ;; a list of valid accounts ;; ... stuff left out ... ( lambda (op . args) ;; lambda inside the let ...))) Common pattern in code Use the let to define state used by the lambda Every use of lambda here will have access to the same students variable lambda inside let
Image of page 14
Caltech CS1 -- Fall 2008 Implementing ;; Continued... (lambda (op . args) ;; message-passing object (cond ;; add a new account to cs1man ((eq? op 'add-student! ) (set! students (cons (car args) students))) ;; return the list of all accounts in cs1man ((eq? op 'get-students ) students) ;; return #t if first argument matches a known account name ((eq? op 'is-student? ) (is-student? (car args))) ;; use helper procedure ))))
Image of page 15

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

View Full Document Right Arrow Icon
Caltech CS1 -- Fall 2008 Usage (define cs1-2008 (make-cs1man-v0.0)) (cs1-2008 ’add-student! ’donnie) (cs1-2008
Image of page 16
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern