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
I know I can't ask you a direct question about the exam, but could you answer the following?
You can ask me a direct question about the exam. I may not answer it, or a I may answer it obliquely. But you are certainly permitted to ask.
Any hints?
I've added hints to a few problems in which people asked for hints, particularly the one on speeding up code. You will also find it useful to look more deeply at the Q and A section of the exam, because the questions people ask can provide hints.
Have two people spent at least four hours on the exam or completed the exam?
Yes. That's among the reasons that there are now only seven problems.
Q (from Sam): What do you expect to get from the expression (> x (and y z))?
(I think this is intended to mean "if x is greater than both y and z".)
I find it useful to think about concrete values: What is (> 3 (and 5 1))?
A (from "volunteer"): We evaluate inside out, so we have to evaluate
(and 5 1). That's probably false because it's senseless. Worse yet, it might be an error message. Perhaps it's 1. It is 1!
andevaluates parameters, left to right, and stops at the first false. If there are no false values, it returns the last one (1).
Let's look at a recursive procedure you've already written: irgb-tally-dark. Goal: Count how many colors in a list of integer-encoded RGB colors are "dark".
How do we start writing a recursive procedure?
(define irgb-tally-dark
(lambda (colors)
(if (_test_)
_the_result_of_the_base_case_
(_do_something_ (irgb-tally-dark (cdr colors))))))
Let's start filing things in
(define irgb-tally-dark
(lambda (colors)
(if (null? colors)
0
(_do_something_ (irgb-tally-dark (cdr colors))))))
Now, if we can tally the rest (thanks magic recursion fairy), we just need to look at the car
(define irgb-tally-dark
(lambda (colors)
(if (null? colors)
0
(if (irgb-dark? (car colors))
(+ 1 (irgb-tally-dark (cdr colors)))
(irgb-tally-dark (cdr colors))))))
Sarah will be upset if you nest if statements.
(define irgb-tally-dark
(lambda (colors)
(cond
[(null? colors)
0]
[(irgb-dark? (car colors))
(+ 1 (irgb-tally-dark (cdr colors)))]
[else (irgb-tally-dark (cdr colors))])))
Let's evaluate
(irgb-tally-dark (list black white black black))
; Is the list null? No
; Is the car dark? Yes.
(+ 1 (irgb-tally-dark (cdr (list black white black black))))
; Do the cdr!
(+ 1 (irgb-tally-dark (list white black black)))
; Is the list null? No
; Is the car dark? No
(+ 1 (irgb-tally-dark (cdr (list white black black))))
; Do the cdr!
(+ 1 (irgb-tally-dark (list black black)))
; Is it null? No.
; Is the car dark? Yes.
(+ 1 (+ 1 (irgb-tally-dark (cdr (list black black)))))
; Do the cdr
(+ 1 (+ 1 (irgb-tally-dark (list black))))
; Is it null? No
; Is the car dark? Yes/
(+ 1 (+ 1 (+ 1 (irgb-tally-dark (cdr (list black))))))
; cdr!
(+ 1 (+ 1 (+ 1 (irgb-tally-dark null))))
; Is null null? Yes!
(+ 1 (+ 1 (+ 1 0)))
(+ 1 (+ 1 1))
(+ 1 2)
3
Can we get Scheme to act more like a human being and count as it goes?
Remaining Count
--------- -----
(black white black black) 0
(white black black) 1
(black black) 1
(black) 2
() 3
We want to write a procedure to keep track of two things, the list of remaining colors and the count of dark colors we've seen so far
(define irgb-tally-dark-colonel
(lambda (remaining tally)
(cond
[(null? remaining)
tally]
[(irgb-dark? (car remaining))
(irgb-tally-dark-colonel (cdr remaining)
(+ 1 tally))]
[else
(irgb-tally-dark-colonel (cdr remaining)
tally)])))
(define irgb-tally-odds-colonel
(lambda (remaining tally)
(display (list 'irgb-tally-odds-colonel remaining tally))
(newline)
(cond
[(null? remaining)
tally]
[(odd? (car remaining))
(irgb-tally-odds-colonel (cdr remaining)
(+ 1 tally))]
[else
(irgb-tally-odds-colonel (cdr remaining)
tally)])))
This model is nice because: