day15

Day15 - Caltech CS1 Fall 2008 CS1 Introduction to Computation Day 15 More Environments& OOP Caltech CS1 Fall 2008 Monty Python and the Holy

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: Caltech CS1 -- Fall 2008 CS1: Introduction to Computation Day 15: November 17, 2008 More Environments & OOP Caltech CS1 -- Fall 2008 Monty Python and the Holy Grail (1975) 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" Caltech CS1 -- Fall 2008 Today • Design a message-passing object implementation • Observe environments it creates • Observe object-oriented programming (OOP) techniques along the way Caltech CS1 -- Fall 2008 cs1man (version 0.0) 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? Caltech CS1 -- Fall 2008 cs1man ops • Constructor – makes a new, empty cs1man object – takes no arguments (define cs1-2008 (make-cs1man)) 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 Caltech CS1 -- Fall 2008 Implementing • This is a good start • Implement this first; come back and add more features later! 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) ...))) 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) 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 Caltech CS1 -- Fall 2008 list? (list? (memq val lst)) • returns #t if val is in lst , otherwise #f • Back to our example... 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 Caltech CS1 -- Fall 2008 Implementing ;; Continued......
View Full Document

This note was uploaded on 09/25/2010 for the course CS 1 taught by Professor Chang during the Spring '08 term at UMBC.

Page1 / 72

Day15 - Caltech CS1 Fall 2008 CS1 Introduction to Computation Day 15 More Environments& OOP Caltech CS1 Fall 2008 Monty Python and the Holy

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

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