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
Will there be more than one section on 161 next semester?
No.
Can we take 207 without taking 161?
No.
Can we take 161 and 207 simultaneously?
No.
So what should I do if I want to continue in CS but can't take 161 next semester?
Ambitious students teach themselves 161 over the summer using the old curriculum. Talk to me separately about how.
Slightly less ambitious students might talk to the people who are teaching conflicting classes about removing the conflict.
Can we do some tricky questions to get ready for the quiz?
What does the following procedure do on an input of '(1 2 3 4 5 6)?
(define tricky
(lambda (lst)
(let kernel ([left null]
[right null]
[remaining lst])
(if (null? remaining)
(list left right)
(kernel right
(cons (car remaining) left)
(cdr remaining))))))
One attempt to solve
left right remaining
() () '(1 2 3 4 5 6)
() '(1) '(2 3 4 5 6)
'(1) '(2) '(3 4 5 6)
'(2) '(3 1) '(4 5 6)
'(3 1) '(4 2) '(5 6)
'(4 2) '(5 3 1)'(6)
'(5 3 1)'(6 4 2)'()
Result
'((5 3 1) (6 4 2))
We might make the base case
(list (reverse left) (reverse right))
'((1 3 5) (2 4 6))
What image do you get from the following?
> (define world (image-show (image-new 200 200)))
> (define t (turtle-new world))
> (turtle-teleport! t 100 100)
> (turtle-face! t 0)
> (repeat 5 (lambda (turtle)
(turtle-forward! turtle 50)
(turtle-turn! turtle 144)))
Write an expression to have a turtle make a regular six-sided star using turtle graphics. You may choose to do two overlapping equilateral triangles or you may choose to just do the outline.
(define world (image-show (image-new 200 200)))
(define t (turtle-new world))
(turtle-teleport! t 100 100)
(turtle-face! t 0)
(repeat 6 (lambda ()
(turtle-forward! t 50)
(turtle-turn! t -60)
(turtle-forward! t 50)
(turtle-turn! t 120)))
Using letrec for the kernel, write a procedure to find the ratio of even to odd numbers in a list of integers. Do ratios using division.
(define even-to-odd
(letrec ([kernel (lambda (evens odds remaining)
(if (null? remaining)
_________________ ; a
(let ([val (car remaining)])
(if (even? val)
_________________ ; b
_________________))))]) ; c
(lambda (lst)
(kernel 0 0 lst))))
One solution
a. (/ evens odds)
b. (kernel (+ 1 evens) odds (cdr remaining))
c. (kernel evens (+ 1 odds) (cdr remaining))
Given that solution, what would a husk that catches inappropriate inputs look like?
(lambda (lst)
(cond
[(not (list? lst))
(error "even-to-odd: expects a list as its parameter, given" lst)]
[(not (all integer? lst))
(error "even-to-odd: expects a list OF INTEGERS as its parameter, given" lst)]
...
[else
(kernel 0 0 lst)]))))
Where is the turtle facing after the following expressions?
(turtle-face! t 0)
(for-each (lambda (x)
(turtle-forward! t 10)
(turtle-turn! t (* 15 x)))
(iota 5))
What is the default size of each brush?
I don't know. It depends on the brush. If the brushes start with a "2. ", you can set their size. Otherwise, it's whatever the brush designer decided on.
How is all defined?
(define all
(lambda (pred? lst)
(or (null? lst)
(and (pred? (car lst))
(all pred? (cdr lst))))))
Or maybe
(define all
(lambda (pred? lst)
(let kernel? ([l lst])
(or (null? l)
(and (pred? (car l))
(kernel? (cdr l)))))))
Main topics:
letrecrepeatfor-eachLocal bindings?
Turtles
Repetition with repeat and for each