Functional Problem Solving (CSC 151 2015S) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] - [Calendar]
Current: [Assignment] [EBoard am] [EBoard pm] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards am] [EBoards pm] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014F)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Book Office Hours] - [Issue Tracker (Course)]
Overview
Situation: Writing a helper procedure using what Sam calls the "column model"
I want to tally all the numbers in a list.
tally remaining
Example: Tally the numbers in the list (list 1 'two "three" 4.0)
tally remaining
----- ---------
0 (list 1 'two "three" 4.0) ; first thing is a number
add 1 throw away the 1
1 (list 'two "three" 4.0) ; first thing is a symbol
keep throw away the 'two
1 (list "three" 4.0) ; first thing is a symbol
Generalize tally remaining ----- --------- 0 lst
Turn into code
(define tally-numbers
(lambda (lst)
(letrec ([kernel (lambda (tally remaining)
(cond
[(null? remaining)
tally]
...))])
(kernel 0 lst))))
This looks significantly different than what we wrote in designing the solution. our solution tied the column names to the initial values; this separates them. As language designers, we can build something better
(define tally-numbers
(lambda (lst)
(let kernel ([tally 0]
[remaining lst])
(cond
[(null? remaining)
tally]
...))))
Make sure to use a relatively small brush. For example,
(context-set-brush! "2. Hardness 100" 2)