Functional Problem Solving (CSC 151 2014F) : EBoards
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Rubric] - [Calendar]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Examples] [Handouts] [Labs] [Outlines] [Readings]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Davis (2013F)] [Rebelsky (2014S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Overview
Did you change the exam?
I added some Q&A.
I added some notes on problem 7.
But the questions themselves did not change.
Can you explain problem 7?
Yes. I'll go over it.
How do I get under 11 "words" in that evil problem?
Look at the reading on anonymous procedures.
In code
(define FUNCTION
(lambda (PARAMS)
(if (EASY? PARAMS)
SIMPLE-SOLUTION
(COMBINE PARAMS (FUNCTION (SIMPLIFY PARAMS))))))
Today: A slightly different pattern
Example: Count the number of objects on the table that aren't tied down.
Objects Running total '(eraser plastic cookies iphone eraser remote) 0 '(plastic cookies iphone eraser remote) 1 '(cookies iphone eraser remote) 2 '(iphone eraser remote) 3 '(eraser remote) 4 '(remote) 5 '() 6 DONE!
Code
(define count-things
(lambda (lst)
(count-things-helper lst 0)))
(define count-things-helper
(lambda (lst counter)
(if (null? lst)
counter
(count-things-helper (cdr lst) (+ 1 counter)))))
Strategy for building these kinds of recursive procedures
Choices of problems to try
Keep track of
List of even numbers I've already seen
Remaining Extracted
'(3 4 5 6 8 11 42) '()
'(4 5 6 8 11 42) '()
'(5 6 8 11 42) '(4)
'(6 8 11 42) '(4)
'(8 11 42) '(6 4)
'(11 42) '(8 6 4)
'(42) '(8 6 4)
'() '(42 8 6 4)
We've run out of elements, keep extracted list
(define extract-evens
(lambda (lst)
(extract-evens-helper lst null)))
(define extract-evens-helper
(lambda (remaining extracted)
(if (null? remaining)
extracted
(exract-evens-helper (cdr remaining)
(if (even? (car remaining))
(cons (car remaining) extracted)
extracted)))))