Functional Problem Solving (CSC 151 2015F) : Assignments
Primary: [Front Door] [Schedule] - [Academic Honesty] [Disabilities] [Email] - [FAQ] [Teaching & Learning] [Grading] [Taking Notes] [Rubric] [Remote Access]
Current: [Assignment] [EBoard] [Lab] [Outline] [Reading]
Sections: [Assignments] [EBoards] [Labs] [Outlines] [Readings] - [Examples] [Handouts]
Reference: [Setup] [VM] [Errors] - [Functions A-Z] [Functions By Topic] - [Racket] [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [Curtsinger (2015F)] [Davis (2013F)] [Rebelsky (2015S)] [Weinman (2014F)]
Misc: [Submit Questions] - [SamR] [Glimmer Labs] [CS@Grinnell] [Grinnell] - [Issue Tracker (Course)]
Assigned: Tuesday, November 3rd, 2015
Due: The due dates for various tasks are as follows.
Important! This examination has an initial code file that you should copy and use as the starting point for your work.
Please read the information on this examination, which includes both instructions and a checklist.Topics: Numeric recursion, documentation, code reading.
Consider the following function definition for a function that takes a real
number, x, and a non-negative integer,
n, as parameters.
(define fun
(lambda (x n)
(cond
[(zero? n)
1]
[(odd? n)
(* x (fun x (- n 1)))]
[else
(square (fun x (/ n 2)))])))
Figure out what this procedure does.
a. Rename fun to something appropriate.
b. Write the 6P-style documentation for the renamed fun.
c. Explain how the procedure achieves its goal.
d. Write a short test suite for the renamed function.
You should have approximately six tests, with at least three different
values for x and at least three different values
for n.
Topics: Integer-encoded RGB colors, recursion, named let, documentation, precondition testing.
In case you've forgotten, one of the simple metrics for the “distance” between two colors is the sum of the squares of the differences between their individual components.
;;; Procedure:
;;; irgb-distance
;;; Parameters:
;;; color1, an integer-encoded RGB color
;;; color2, an integer-encoded RGB color
;;; Purpose:
;;; Find the distance between color1 and color2, using some simple
;;; metric for distance.
;;; Produces:
;;; distance, a non-negative real number
;;; Preconditions:
;;; [No additional]
;;; Postconditions:
;;; If color1=color2, then distance is 0
;;; For any three colors, a, b, and c, if
;;; (irgb-distance a b) < (irgb-distance b c)
;;; then a is likely to be perceived as being closer to b than c.
;;; Plus:
;;; irgb-distance is commutative. That is, for any two colors, a and b,
;;; (irgb-distance a b) = (irgb-distance b a)
(define irgb-distance
(lambda (color1 color2)
(+ (square (- (irgb-red color1) (irgb-red color2)))
(square (- (irgb-green color1) (irgb-green color2)))
(square (- (irgb-blue color1) (irgb-blue color2))))))
a. Document a procedure, (, that
finds the element of irgb-furthest
color colors)colors that is
furthest from color
using irgb-distance as the distance for the metric.
b. Implement irgb-furthest.
You must use named let for a recursive kernel for this procedure.
Here's a start.
(define irgb-furthest
(lambda (color colors)
(let kernel ([remaining (cdr colors)]
[guess (car colors)])
...)))
c. Add appropriate precondition testing so that
irgb-furthest issues an appropriate error message
when it receives incorrect inputs. You should strive to make sure
that the error messages clearly correspond to the different kinds of
errors that might occur.
Topics: Strings, numeric recursion.
Write but do not document a procedure, (, that takes a string string->words
str)str as input and returns a list of strings, each containing one word from the input string. You may assume words are separated by spaces.
Here are some example uses of .
string->words
> (string->words "Hello World")
'("Hello" "World")
> (string->words "Words may include punctuation. That is okay!")
'("Words" "may" "include" "punctuation." "That" "is" "okay!")
> (string->words "")
'("")
> (string->words "oneword")
'("oneword")
> (string->words "two spaces")
'("two" "" "spaces")
Topics: Strings, higher-order procedures,
map, documentation.
Some time ago, a student who I will call “Gle” came up with something like the following as an alternative to an existing procedure.
(define f (lambda (a b c) (list->string (map (o (l-s string-ref a) (l-s + b)) (iota (- c b))))))
Figure out what the procedure does and then do the following.
a. Rename the procedure and the parameters so that their type and purpose is clearer.
b. Reformat the code.
c. Document the updated procedure.
Topics: Turtle graphics, using actions, for-each
Sometimes it's useful to make lists of actions for a turtle to complete.
For example, to make a triangle, we can have "forward 10, turn 120,
forward 10, turn 120, forward 10". In Scheme, we might represent each
action as a one-parameter procedure. For example, (r-s turtle-forward!
10) or (lambda (t) (turtle-teleport! t 100 100)).
Write but do not document a procedure,
( that has the turtle follow a
script, where the script is a list of one-parameter procedures.
You may not use explicit recursion.
turtle-script! turtle
script!)
Here's the documentation for turtle-script!.
;;; Procedure: ;;; turtle-script! ;;; Parameters: ;;; turtle, a turtle ;;; script, a list of one-parameter functions ;;; Purpose: ;;; Have turtle execute the actions in script one by one ;;; Produces: ;;; turtle, the same turtle ;;; Preconditions: ;;; script has the form '(action-1! ... action-n!) ;;; Each action is a procedure of one parameter that takes a turtle ;;; as input. ;;; Postconditions: ;;; The following actions have been executed in turn ;;; (action-1! turtle) ;;; (action-2! turtle) ;;; ... ;;; (action-n! turtle)
Here are a few examples.
![]() |
(turtle-script! (turtle-new (image-show (image-new 200 200)))
(list (section turtle-teleport! <> 100 50)
(r-s turtle-face! 90)
(r-s turtle-forward! 80)
(r-s turtle-set-color! "red")
(r-s turtle-turn! -45)
(r-s turtle-forward! 30)
(r-s turtle-set-color! "grey")
(r-s turtle-turn! -90)
(r-s turtle-forward! 60)
(r-s turtle-set-color! "blue")
(r-s turtle-face! 180)
(r-s turtle-forward! 100)))
|
![]() |
(define side-script
(list (r-s turtle-forward! 100)
(r-s turtle-turn! 90)))
(define square-script
(append (list (lambda (t) (turtle-teleport! t 50 50))
(r-s turtle-set-color! "green"))
side-script side-script side-script side-script))
(turtle-script! (turtle-new (image-show (image-new 200 200)))
square-script)
|
Topics: Turtle drawings, recursion, recursion with helpers, numeric recursion, documentation.
Write and document a procedure, (, that uses the provided turtle-tree! turtle trunk-length branches levels)turtle to draw a tree.
You can think of a tree as a trunk (a line) with some number of smaller trees "growing" out of the end of the trunk.
To draw a tree, your turtle should rotate to a random angle between plus and minus 60 degrees, then draw a line that is trunk-length pixels long.
The end of the trunk should split into some number of smaller trees, determined by the branches parameter.
Each smaller tree should have a slightly shorter trunk length (75% of the previous trunk length works well).
This process should repeat until the tree has levels levels;
for example, if branches and levels are both 3, the tree should have a total of 13 lines.
Before drawing a tree, some set-up is required:
; Create a world, a turtle, set the brush to a fine line, and position the turtle
; at the bottom middle of the image facing up.
(define set-up-turtle
(lambda ()
(let* ([world (image-show (image-new 300 200))]
[tommy (turtle-new world)])
(turtle-turn! tommy -90)
(turtle-teleport! tommy 150 200)
(turtle-set-brush! tommy "2. Hardness 100" 0.15)
tommy)))
After running the set-up code above, you should be able to run the following examples with a completed turtle-tree! function.
![]() |
; Draw a tree with a trunk length of 60, three branches, and three levels. (turtle-tree! (set-up-turtle) 60 3 3) |
![]() |
; Draw a larger tree, this time with five branches and four levels. (turtle-tree! (set-up-turtle) 60 5 4) |
![]() |
; Draw another large tree. Trees don't always grow straight! (turtle-tree! (set-up-turtle) 60 5 4) |
![]() |
; Draw a very large tree with seven branches and five levels. This will take quite a while. (turtle-tree! (set-up-turtle) 60 7 5) |
Here we will post answers to questions of general interest. Please check here before emailing your questions!
STUB comment that appears in the code
file?
(string->words "Hello world ") return? In other words, how should we handle extra spaces at the end of a string?
'("Hello" "world") or '("Hello" "world" ""). The result '("Hello" "world ") would not receive full credit; string->words should never return words that contain spaces.
for-each or repeat, you may be
implicitly be using recursion. Implicit recursion is okay.
(define apply-to-4
(lambda (proc)
(proc 4)))
> (apply-to-4 square) 16 > (apply-to-4 sqrt) 2 > (apply-to-4 increment) 5 > (apply-to-4 (r-s expt 3)) 64 > (apply-to-4 (lambda (val) (* 3 (+ val 2)))) 18
for-each,
so think about the types of parameters to for each. One of those
parameters is a procedure. What is the input type to that procedure?
[2015-11-07]
Here you will find errors of spelling, grammar, and design that students have noted. Remember, each error found corresponds to a point of extra credit for everyone. We usually limit such extra credit to five points. However, if we make an astoundingly large number of errors, then we will provide more extra credit. (And no, we don't count errors in the errata section or the question and answer sections.)
trunnk-length instead of
trunk-length in the sample code file.
[RS, GN, and ZS, 1/2 point]
Some of the problems on this exam are based on (and at times copied from) problems on previous exams for the course. Those exams were written by Janet Davis, Rhys Price Jones, Samuel A. Rebelsky, John David Stone, Henry Walker, and Jerod Weinman. Many were written collaboratively, or were themselves based upon prior examinations, so precise credit is difficult, if not impossible.
Some problems on this exam were inspired by conversations with our students and by correct and incorrect student solutions on a variety of problems. We thank our students for that inspiration. Usually, a combination of questions or discussions inspired a problem, so it is difficult and inappropriate to credit individual students.