Functional Problem Solving (CSC 151 2013F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning] [Grading]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Setup] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2010F)] [Weinman (2012F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)]
Overview
How do I tell if my work got uploaded?
No errors -> It probably worked. Sam will email you if it didn't.
One thing we do again and again and again is represent data
All of these group pieces together.
Some are mutable.
We want ways to represent thse within our programming language.
We can use vectors. E.g., for turtles we might decide that
We could also use a list
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
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>
>
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] [FAQ] [IRC] [Teaching & Learning] [Grading]
Current: [Assignment] [EBoard] [Lab] [Outline] [Partners] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Partners] [Readings]
Reference: [Setup] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2010F)] [Weinman (2012F)]
Misc: [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] [Issue Tracker (Course)]
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.)

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.