home—lectures—exams—hws—breeze (snow day)
hw03
handling lists
due Sep.23 (Fri)
Due Sep.23 (Fri) (hardcopy, and on D2L)
Your name and the assignment-number
must be in a comment at the start of the file.
I strongly suggest having finished #7 (count-bigs) before the next lecture,
as well as #9-10.
The reading-questions are lower priority.
Reading:
-
Chpt.15, review question #9, the two forms of define. (If using 8ed: #5)
-
Chpt.15, review question #11. (If using 8ed: Why are car and cdr so named?)
-
Chpt.15, review question #13, syntax,semantics of let. (If using 8ed: #7)
-
Chpt.15, review question #16. (If using 8ed: What is tail recursion, and why is it desirable?)
-
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.”.)
-
In that essay, what feature of Lisp does Paul Graham present as
the one which makes it more powerful than most others?
-
Write the scheme function count-bigs : number, (listof number) → natnum1,
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)
|
-
Write the function mapsqr : (listof number) → (listof number),
which squares each number in a list:
(check-expect (mapsqr empty) empty)
(check-expect (mapsqr (cons 7 empty)) (cons 49 empty))
(check-expect (mapsqr (cons 9 (cons 7 empty))) (cons 81 (cons 49 empty)))
|
-
The static Java method move will
which takes in a Ship,
and returns a whole new (different!) Ship object which has traveled for
one unit of time (a "tick").
-
Write a stub method.
- Write two test cases.
(You'll need to print out the (a) actual and (b) expected results
of calling your method; presumably2 you'll want to
write a toString to help with this.)
You can put those test cases in Ship.main,
or in a separate test-driver class if you prefer.
You might want to review notions of distance vs. acceleration:
one site is
u3l2c.cfm.
(Post on the message boards, if you find a site/explanation you like better.)
We aren't actually dealing with acceleration at all, yet, but we will.
You do not need to worry about the screen-size
or wrapping around for this hw
(though you can add that if you like; you'll eventually need it).
- After writing the test cases and getting them to compile,
now complete the method.
You should have a single .java file (which compiles
and runs);
it should be less than a page of well-spaced, commented code.
-
In your racket version,
write the corresponding move-ship function
(including test-cases of course, presumably using check-expect).
-
Continue your racket version
by implementing a struct for asteroids.
(Hint: Give it a shorter name,
something like “stroid”.)
(For every field, give a two-to-three word description of what
it stands for, and its units.)
-
Write move-astr,
which takes in an asteroid and returns an asteroid one tick later.
(If you called your struct “stroid”, then this would
presumably be called “move-stroid”.)
-
Write move-astrs : (listof astr) -> (listof astr),
which updates all asteroids in a list.
The following three problems can be deferred until next week's hw,
although you can certainly get started on them earlier!
If you want to test these functions, you can either call empty-scene,
or use any previous image you've made.
You don't need check-expects for the image functions
(though you are certainly welcome to do so).
-
Write a function draw-ship which takes in a ship and an image (the screen / background),
and returns a new image which contains [a picture of] the ship overlayed on the given background.
Include test cases, before writing the code!
Look in the racket documentation for helpful image-drawing functions.
(Either search directly,
or take a function you know about (like circle), put the caret over it,
and hit F1 to call up the documentation.)
-
Write draw-astr : astr, image -> image.
-
Write draw-astrs : (list-of astr), image -> image.
1
I'll use “natnum” for “natural number” —0,1,2,…. ↩
2
If you want to be fancier than just printing out
results, you can use JUnit tests.
This will require overriding equals.
↩
home—lectures—exams—hws—breeze (snow day)