Functional Problem Solving (CSC 151 2013F) : EBoards

CSC151.02 2013F, Class 50: Objects in Scheme


Overview

Preliminaries

Admin

Questions on the Project

How do I tell if my work got uploaded?

No errors -> It probably worked. Sam will email you if it didn't.

Questions on the Exam

Motivating problems: Circles, turtles, and counters

One thing we do again and again and again is represent data

All of these group pieces together.

Some are mutable.

Building and using compound values

We want ways to represent thse within our programming language.

Simpler example: Counters:

Code!

(define make-counter
  (lambda (name)
    (vector 0 name)))
(define display-counter
  (lambda (counter)
    (display (vector-ref counter 1))
    (display ": ")
    (display (vector-ref counter 0))
    (newline)))
(define increment-counter!
  (lambda (counter)
    (vector-set! counter 0 (+ 1 (vector-ref counter 0)))))

Welcome to DrRacket, version 5.2.1 [3m].
Language: racket; memory limit: 128 MB.
> (define sam (make-counter 'sam))
> (increment-counter! sam)
> (display-counter sam)
sam: 1
> (vector-set! sam 0 'samuel)
> (increment-counter! sam)
. . +: expects type <number> as 2nd argument, given: 'samuel; other arguments were: 1

Objects: A new approach to compound values

Creating objects in Scheme

Key idea: You can have functions with private data

(define sam
  (let ((x (vector 0 'sam)))
    (lambda ()
      (display (vector-ref x 1))
      (display ": ")
      (display (vector-ref x 0))
      (newline)
      (vector-set! x 0 (+ 1 (vector-ref x 0))))))

(define samr
  (let ((x (vector 0 'sam)))
    (lambda (command)
      (cond
        ((equal? command ':describe)
         (display (vector-ref x 1))
         (display ": ")
         (display (vector-ref x 0))
         (newline))
        ((equal? command ':increment!)
         (vector-set! x 0 (+ 1 (vector-ref x 0))))
        ((equal? command 'seuss)
         (display "I do like green eggs and ham."))
        (else
         (error "I do not know how to " command))))))

Note: The let/lambda combination creates a local variable that is accessible only within the procedure and that lives until the Scheme session ends.

Complexity: We don't want single objects, we want functions that build objects.

(define counter-new
  (lambda (name)
    (let ((vec (vector 0 name)))
      (lambda (command)
        (cond
          ((equal? command ':describe)
           (display (vector-ref vec 1))
           (display ": ")
           (display (vector-ref vec 0))
           (newline))
          ((equal? command ':increment!)
           (vector-set! vec 0 (+ 1 (vector-ref vec 0))))
          ((equal? command 'seuss)
           (display "I do like green eggs and ham."))
          (else
           (error "I do not know how to " command)))))))

> (define a (counter-new 'alpha))
> (define b (counter-new 'beta))
> (a ':describe)
alpha: 0
> (b ':increment!)
> (b ':increment!)
> (a ':describe)
alpha: 0
> (b ':describe)
beta: 2
> a
#<procedure>
> b
#<procedure>
>

Samuel A. Rebelsky, rebelsky@grinnell.edu

Copyright (c) 2007-2013 Janet Davis, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials are copyright by John David Stone or Henry Walker and are used with permission.)

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.