home—lectures—exams—hws—breeze (snow day)
hw03
handling lists
due Sep.26 (Wed)
Due Sep.26 (Wed) in class.
Your name and the assignment-number
must be in a comment at the start of the file,
and your hardcopy must be stapled.
Reading:
- (15pts)
The following questions relate to the beginning stages of
designing an
Asteroids-like game.
Be sure to play this game for a few moments, thinking about how what data type(s) you'll
use to represent it — that is, what structures will you define,
and what fields will they have (and what are the types of each field).
(For this problem, you'll submit two files:
a .java and .rkt program file.
Make these separate files from the answers above.)
We start by representing the player's ship, and writing a method involving them.
- (Complete this by Sep.24 (Mon); we will discuss it that day in class.)
Give a Java class to represent a ship
(non-static fields and constructor only).
Note that no images are involved.
(For every field, give a two-to-three word description of what
it stands for, and its units.)
-
Give some Java statements which
create two different Ship objects
-
Give a corresponding Racket define-struct statement.
-
What is the signature of of the constructor which racket creates for you?
(A function's signature is its name, along with the types of its parameters and its return type.
For example,
the signature of
substring is substring : string, int, int → string
and
the signature of
addition is + : number, number → number.
- (2pts)
Give two examples of calling the constructor, corresponding to your the two Java examples.
-
What is the name and signature of one of the ship accessor functions which racket creates for you?
-
Create (in both racket and Java),
a function ship->speed,
which returns the speed of a given ship
(something incorporating both the x- and y- velocities).
Be sure to include three test cases, one of which should be non-trivial.
- (3 pts)
Chpt.15, review question #9, the two forms of define. (If using 8ed: #5)
(For each form, give (a) an example, (b) the general syntax
1,
and (c) a one-sentence explanation of the semantics.
That is, the same way cond was presented in lecture.)
In your syntax-definition,
you can refer to “Expr” (expression),
“Id” (identifier),
and “…” (0 or more of the preceding).
- (3pts)
Chpt.15, review question #11. (If using 8ed: Why are car and cdr so named?)
- (3pts)
Give a concise, 1-2 sentence paraphrasing of Paul Graham's “Blub paradox”.
You can assume the premise (without re-stating it):
Consider “a hypothetical language called Blub.
Blub falls right in the middle of the abstractness continuum.
It is not the most powerful language, but it is more powerful than Cobol or machine language.”.)
- (3pts)
In that essay, what feature of Lisp does Paul Graham present as
the one which makes it more powerful than most others?
- (3pts)
Write the racket function count-bigs : number, (listof number) → natnum2,
which takes in a threshold and a list of numbers, and returns how many of them
are larger than the threshold.
(check-expect (count-bigs 7 empty) 0)
(check-expect (count-bigs 7 (cons 5 empty)) 0)
(check-expect (count-bigs 7 (cons 7 empty)) 0)
(check-expect (count-bigs 7 (cons 9 empty)) 1)
(check-expect (count-bigs 7 (cons 3 (cons 7 empty))) 0)
(check-expect (count-bigs 7 (cons 9 (cons 7 empty))) 1)
(check-expect (count-bigs 7 (cons 3 (cons 9 empty))) 1)
(check-expect (count-bigs 7 (cons 8 (cons 9 empty))) 2)
(check-expect (count-bigs 7 (cons 3 (cons 7 (cons 8 (cons 2 empty))))) 1)
|
- (3pts)
Write the racket function map-sqr : (listof number) → (listof number),
which squares each number in a list:
(check-expect (map-sqr empty) empty)
(check-expect (map-sqr (cons 7 empty)) (cons 49 empty))
(check-expect (map-sqr (cons 9 (cons 7 empty))) (cons 81 (cons 49 empty)))
|
1
Please distinguish between what words/punctation are required, and what gets
replaced with a certain feature.
For example, the syntax of an if-expression is
(if Expr Expr Expr),
where each of the Exprs can be any expression.
Note that the “(if” is required punctuation/keyword
and is something the programmer literally types in,
whereas
“Expr” (in different font/color)
is something that the programmer replaces with some particular expression;
they don't type E-x-p-r literally.
↩
2
I'll use “natnum” for “natural number” —0,1,2,…. ↩
home—lectures—exams—hws—breeze (snow day)