0% found this document useful (0 votes)
60 views9 pages

Practice Quiz 2 Solutions

The document provides solutions to a practice quiz with 11 multiple choice questions about Racket expressions. For each expression, the user must identify the type of value produced (number, list, Boolean, exception) or specify which procedure call would cause an exception. The second part of the quiz contains expressions that produce errors, and the user must rewrite the expressions to produce the desired output. A glossary of Racket predicates and operations is also provided.

Uploaded by

Boyu Ran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views9 pages

Practice Quiz 2 Solutions

The document provides solutions to a practice quiz with 11 multiple choice questions about Racket expressions. For each expression, the user must identify the type of value produced (number, list, Boolean, exception) or specify which procedure call would cause an exception. The second part of the quiz contains expressions that produce errors, and the user must rewrite the expressions to produce the desired output. A glossary of Racket predicates and operations is also provided.

Uploaded by

Boyu Ran
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Write your name here:

Practice Quiz 2 Solutions


Part one (5 points per question)
For each series of expressions below, give the type output by the last of the expressions. That is,
if its value is a number, say “number”. If it’s a list, say “list”, if it’s a Boolean, say “Boolean”, etc.
If the result is an exception of some sort, say “Exception”. You do not need to specify what kind
of exception it would be, however do specify what procedure call generated the exception, where
appropriate.

1. [define [list-max list]


[if [= [length list] 1]
[first list]
[max [first list]
[list-max [rest list]]]]]
[list-max [list 1 2 3 4 1]]

Answer: Number

2. ‘[]

Answer: (empty) List

3. [+ 1 “2”]

Answer: Exception
Explanation: you can’t add a number to a string.

4. [filter odd? [list 1 “2” 3 4 5]]


Answer: Exception
Explanation: Filter calls odd? on the list element “2”, which isn’t a number.

5. [+ 1 2
[if false “2” 3]]
Answer: number

6. [define [my-form]
[define f [make Form]]
form]
[my-form]

Note: assume the user has already executed [using System.Windows.Forms], so that the
Form class is available.

Answer: Exception
Explanation: there’s no variable defined called form.

7. [define x 0]
[define [mystery]
[with x = 5
[x ← “test”]]
x]

EECS-111 Fall 2009


Write your name here:

[mystery]
Answer: number
Explanation: there are two variables called x here, the global variable x, which is a
number, and the local variable x, which starts as a number and is changed to a
string. The assignment statement, which changes x to a number occurs within the
scope of the local variable, so the local variable is changed, leaving the global
variable unchanged. However, the x which appears at the end of mystery is
outside the with, and so outside the scope of the local variable x. It therefore
refers to the global variable, which is still a number.

8. [define test [generic-procedure]]


[define-method [test [Number x]]
x]
[define-method [test [Integer x]]
“Integer”]
[any string?
[map test [list 1.5 2 2.5]]]
Answer: Boolean (true, in this case)
Explanation. When test is called with an integer, it returns the string “Integer”, but
when it’s called with any other kind of number, the more general method is used,
and so it returns the original number. So when we map test over a list of numbers,
it returns strings for the integers and numbers for the non-integers. Since the list
contains integers, any is going to return true.

9. [define [my-form]
[define result 0]
[define f [make Form]]
[define button1 [make Button
Click: [ignore ignore →
[result ← “Button1”]]]]
[define button2 [make Button
Top: 40
Click: [ignore ignore →
[result ← false]]]]
[f.Controls.Add button1]
[f.Controls.Add button2]
[Application.Run f]
result]
[my-form]

Note: Assume this procedure runs properly and that the user presses button1 followed by
the close box. Why type of value does it return?
Answer: String
Explanation: when the user presses button1, the Click method for button1 is run,
which changes result to a string. After the form is closed, Application.Run returns
and the procedure returns the value of result, which is now a string.

10. [define [foo list]


[with x = 0
[while [not [empty? list]]
[x ← [+ x [first list]]]
[list ← [rest list]]]
x]]
[foo [list 1 2 3]]

EECS-111 Fall 2009


Write your name here:

Answer: number (6)

11. [define foo [generic-procedure]]


[define-method [foo [String x]]
x]
[define-method [foo [Integer x]]
[+ x 1]]
[foo 0.5]
Answer: Exception (NoMatchingMethodException)
Explanation: 0.5 is neither an integer nor a string, so foo has no matching method.

EECS-111 Fall 2009


Write your name here:

Part two (10 points per question)


Below are a series of expressions that produce errors or otherwise fail, along with the desired
output the programmer had intended to produce. Give the correct form of the expression to
produce the desired output. You may either rewrite the expression from scratch or just write
corrections on top of the original.

1. [fold +
[map sublis → [fold + sublis]
[list [list 1 2 3] [list 4 5 6] [list 7 8 9]]]]

Desired output: The sum of all the numbers in all the sublists, i.e. 6+15+24=45.
Error: ArgumentTypeException in call to fold (list argument was a procedure rather than a
list).

Answer: add brackets as follows


[fold +

[map [ sublis → [fold + sublis] ]


[list [list 1 2 3] [list 4 5 6] [list 7 8 9]]]]

Explanation: by omitting the brackets around the procedure, it fooled the system
into thinking the map expression was one big procedure with map and sublis as its
inputs

2. [define [power base exponent]


[if [= exponent 0]
1
[× base
[power base
[+ exponent 1]]]]]
[power 2 3]

Desired output: 2 raised to the 3rd power, i.e. 8.


Error: stack overflow

Answer: change the + to a -


Explanation: by increasing the exponent on each call rather than decreasing it, the
recursion brings the exponent farther away from termination (i.e. exponent=0)
rather than closer.

3. [define [find-files name directory]


[define result [new ArrayList]]
[define [recur directory]
[for-each [file → [when [string-contains? name file]
[result.Add file]]]
[Directory.GetFiles directory]]
[for-each recur [Directory.GetDirectories directory]]]
[recur directory]]

Desired output: a list of all the files in directory whose pathnames contain name.
Note: assume string-contains? returns true if and only if the string file contains

EECS-111 Fall 2009


Write your name here:

the string name. Also, remember that the Add method adds an element to the
end of an ArrayList.
Error: None. It runs fine, but it doesn’t return any value at all.
Answer: add result to the end.
Explanation: the code is fine. It properly builds the result list up, it just doesn’t return
its value.

4. [define [iterated-group proc count]


[define result [group]]
[while [= count 0]
[count ← [− count 1]]
[result ← [group [proc count] result]]]
result]
[iterated-group [n → [box [× n 10]
[× n 10]]]
10]

Desired output

Error: None, but it always returns a blank picture.

Answer: change = to >


Explanation: the test on the while loop only allows the body of the loop to run if count
is 0. But we want it to stop at 0. So the body of the loop never runs and so the result
is always returned unchanged, and result is initialized to [group], which is a blank
picture.

5. [define [max-list list]


[define max [first list]]
[list ← [rest list]]
[while [not [empty? list]]
[if [> [first list] max]
[max ← [first list]]]]
max]
[max-list [list 1 2 3]]

Desired output: the largest element of the list, i.e. 3.


Error: None, but the program runs forever without ever outputting a value.
Note: if you aren’t sure how to fix the program, be sure to at least explain why
it never stops.

Answer: add [list <- [rest list]] to the end of the while loop

EECS-111 Fall 2009


Write your name here:

Explanation: the while loop never changes list and so it can never
become empty.

EECS-111 Fall 2009


Write your name here:

Glossary

Predicates
[and booleans …]
[or booleans …]
Returns true if all/any of the booleans are true.

[not boolean]
Returns true if argument is false, or false if argument is true.

[= object1 object2]
Returns true if object1 and object2 are the same.

[< x y], [≤ x y], [> x y], etc.


Returns true if x is less than, less-than-or-equal-to, or greater than y, respectively.

[string? x], [number? x], [procedure? x], [is? x type]


True if x is of the specified type.

[odd? integer], [even? integer]


Returns true if the integer is odd or even, respectively.

Arithmetic
[+ numbers …]
[+ vectors …]
[+ colors …]
Returns the sum of the given numbers, colors, or vectors. Numbers, colors, and vectors cannot
be mixed.

[− number number]
[− vector vector]
[− color color]
Returns the different of two numbers, vectors, or colors.

[− number]
[− vector]
Returns the number/vector times -1.

[× number number/color/vector]
[× number/color/vector number]
[ ∕ number/color/vector number]
Returns the specified multiple (or quotient) of the specified number, color, or vector, and the
specified number.

[abs number]
Returns the absolute value of number, i.e. the number with the sign erased.

[sin number], [cos number], [√ number]


Returns the sine, cosine, or square root of the number, respecitively.

EECS-111 Fall 2009


Write your name here:

Colors
[color number number number]
Returns a color with the specified amounts of red, green, and blue, respectively. If any number is
less that 0, it is treated as 0. If any number is greater than 255, it is treated as 255.

[color name]
Returns the color with the specified name. The name should be a string, such as “red” or “green”.
Strings are typed in double quotes: “”.

[red color], [green color], [blue color] (or: color.R, color.G, color.B, respectively)
Returns the amount of red, green, or blue light in the specified color as a number from 0-255.

Vectors (aka points)


[point number number], [vector number number]
Returns a vector/number with the specified X and Y coordinates, respectively.

[point-x vector], [point-y vector]


(or: [vector-x vector], [vector-y vector]
vector.X, or vector.Y, respectively)
Returns the X- or Y- component of the specified vector (aka point).

Pictures
All the following procedures return pictures. Boxes, ellipses, groups, etc. are particular kinds of
pictures. However, bitmaps can not be given as inputs to the procedures in this section.

[box width height], [ellipse width height]


Returns a box or ellipse of the specified width and height (numbers).

[line point point]


Returns a line between the specified points

[group pictures …]
Returns a picture comprised of all the pictures passed as arguments. The pictures can be boxes,
ellipses, lines, groups, or any other object returned by procedures in this section. When called
with no arguments, group returns a blank picture.

[translate vector pictures …]


Returns a composite picture of all the specified pictures and shifts it by vector.

[rotate angle pictures …]


Returns a composite picture of all the specified pictures and rotates it by angle.

[paint color pictures …]


Returns a composite picture of all the specified pictures and fills all the objects with the specified
color.

[paint [pen color width]


pictures …]
Returns a composite picture of all the specified pictures and outlines all the objects with the
specified color and pen width (a number).

[iterated-group picture-procedure count]


Makes a compound picture by calling picture-procedure repeatedly with inputs ranging from 0 to

EECS-111 Fall 2009


Write your name here:

count-1. The resulting picture contains all the pictures returned by all the calls to picture-
procedure. Thus picture-procedure must take a number as input and return a picture as its result.

Lists
[empty? list]
Returns true if list has no elements.

[list elements …]
Returns a list with all the specified elements, in order. If called with no arguments, it returns the
empty list.

[first list], [second list], etc.


[item list position]
Returns the specified element of the list

[rest list]
Returns all but the first item of the list

[cons item list]


Returns a list that begins with item and continues with the elements of list. That is, it makes a
copy of list with item added to the beginning.

[map procedure list]


[for-each procedure list]
Calls procedure on each element of list. Map returns all the results as a list. For-each returns
nothing (more properly, it returns a useless value).

[filter procedure list]


Returns only those elements of list for which procedure returns true.

[fold procedure start list]


Runs procedure on successive elements of lists, starting with start. For example, [fold + 0 [list 1 2
3]] returns the sum of 1, 2, and 3.

[up-to count procedure]


Calls procedure for all values from 0 to count-1 and returns the results as a list. That is, it’s
roughly equivalent to: [list [procedure 0] [procedure 1] … [procedure [– count 1]]].

[any predicate list]


[every predicate list]
Calls predicate on elements of list. Returns true if predicate returns true for any/every element of
the list.

[apply procedure list]


Runs procedure, taking its arguments from the elements of list. For example, [apply + [list 1 2 3]]
is equivalent to [+ 1 2 3].

File system access


[Directory.GetFiles dir]
[Directory.GetDirectories dir]
Returns a list of the paths for all the files/directories in dir.

EECS-111 Fall 2009

You might also like