home—info—lectures—exams—archive
hw02
Scheme warm-up
Due 2008.Sep.19 (Fri) in class.
Turn in: a hard-copy of your programs,
as well as an electronic copy of a single file
(suffix “.ss”) on Blackboard(WebCT).
See scheme-resources—ITEC380 Scheme resources
for reading links,
and information on running scheme.
- (5pts)
Programming Exercise 15.1 (volume of a sphere).
-
(5pts)
In DrScheme, select Language > Add Teachpack > world.ss.
(When you next hit Run, the interactions window
should mention this teachpack along with the language-version etc..)
Read about the following functions in DrScheme's helpdesk,
and try them out in the interactions window:
Using those library functions, create (compute) the following values:
- An empty scene with the image of a small fire placed on it.
Name this scene (using define)
“fire1”.
(To get a gif/jpeg into DrScheme IDE,
use Insert > Insert image….
You'll want to pass that image as one of the arguments
to place-image.)
You can use an image as shown at the bottom of
the upcoming fire-fighting-game assignment.
- The scene fire1
with one blue circle (water droplet) placed onto it at
(at a position of your choice).
Name this scene drop1a.
- The scene fire1
with a water droplet in a different location:
one pixel lower than in drop1a.
Name this scene drop1b.
- Optional1
create an image like drop1a with two more drops.
Write this as one expression, without using additional defines.
Notice how place-image returns a new image;
it doesn't modify its input values.
- (5pts)
Define a structure to represent an airplane, with the following
pieces of information:
- x,y position
- its direction: flying either to the left of the screen,
or to the right.
- its image (which should jibe with its indicated direction;
you can use the reflect-v (provided below)
if you have an image which you'd like to reflect vertically.)
Construct three2 example airplanes:
-
One example at any location, direction, picture you choose.
-
the second should be just like the first,
except it is one pixel to the right
(assuming the plane is moving to the right).
-
The third is just like the second except that
it is moving in the opposite direction.
(You can call reflect-v below,
to generate a reflected version of your first image.)
- (5pts)
Write the function
draw-airplane : (-> airplane scene scene).
Note:
the function reflect-v : (-> image image),
which creates a mirror-image,
is provided here.
If you used a symbol to represent the plane's direction,
you'll want to use built-in primitive
symbol=?.
If you used a boolean, you might (but don't need to) use
boolean=?.
-
Write the function
set-airplane-direction : (-> airplane symbol airplane),
which consumes an airplane and a symbol
(which is either 'left or 'right),
and returns an airplane structure with the indicated direction.
Use the provided reflect-v as needed,
to reflect the airplane's image.
- (5pts)
Programming Exercise 15.4 (count-zeroes).
Use only the functions mentioned in the book
(updating to the book's old-school
dialect).
(Don't, for example, use scheme's built-in filter.)
Here are several test cases; provide at least two more as directed:
(check-expect (count-zeroes empty) 0)
(check-expect (count-zeroes (cons 7 empty)) 0)
(check-expect (count-zeroes (cons 0 empty)) 1)
;(check-expect (count-zeroes (cons 2 (cons 0 empty))) 1))
(check-expect (count-zeroes (cons 2 (cons 0 empty))) 1) ;; corrected (Sep.17 20:30)
;;; PROVIDE TWO MORE TEST CASES (using ‘cons’, not ‘list’)
|
- (5pts)
Write subst:
; subst: (-> atom atom (list-of atom) (list-of atom))
;
(define (subst old new data)
...)
(check-expect (subst 'apple 'zinger (list 'apple 'banana))
(list 'zinger 'banana))
(check-expect (subst 'apple 'zinger (list 'cherry 'banana))
(list 'cherry 'banana))
(check-expect (subst 'apple 'zinger (list 'apple 'banana 'apple 'kiwi 'apple))
(list 'zinger 'banana 'zinger 'kiwi 'zinger))
;;; PROVIDE ADDITIONAL TEST CASES (which can be simpler than the above)
;; Clarification (Sep.17 (Wed) 15:15):
;; (list 'a 'b 'c)
;; returns
;; (cons 'a (cons 'b (cons 'c empty))).
;; These two functions are related *but different*;
;; `list` tends to be useful for test-cases, while `cons` is usually what
;; you want inside your code.
|
Use only functions mentioned in the book
(updating to the book's old-school
dialect).
- (5pts)
Write insert-sort.
(Hint: This follows directly from the design-recipe
from How to Design Programs,
which was also mentioned in lecture.)
Good design suggests using a helper-function,
to combine the result of the recursive call with
the first element.
Include adequate test-cases for your helper function.
; The original version had a mis-placed parenthesis; fixed here.
(check-expect (insert-sort empty) empty)
(check-expect (insert-sort (list 30)) (list 30))
(check-expect (insert-sort (list 20 30)) (list 20 30))
(check-expect (insert-sort (list 30 20)) (list 20 30))
(check-expect (insert-sort (list 20 30 40)) (list 20 30 40))
(check-expect (insert-sort (list 20 40 30)) (list 20 30 40))
(check-expect (insert-sort (list 30 20 40)) (list 20 30 40))
(check-expect (insert-sort (list 30 40 20)) (list 20 30 40))
(check-expect (insert-sort (list 40 30 20)) (list 20 30 40))
(check-expect (insert-sort (list 40 20 30)) (list 20 30 40))
|
(Note that some of the previously-pending problems have been deferred
to hw03.)
If you're curious,
notes and more comprehensive test cases are
given here.
; reflect-v : (-> image image)
; Reflect an image vertically (that is, make a mirror image).
;
(define (reflect-v img)
(let* {[w (image-width img)]
[h (image-height img)]
[new-pinhole-x (- (image-width img) (pinhole-x img) 1)]
}
(color-list->image (reflect-list-2d-v (image->color-list img) w h)
w h
new-pinhole-x (pinhole-y img))))
; list-ref-2d : (-> (list-of any) int int int int)
; index into a list, as if it were a w-by-h 2-D array.
;
(define (list-ref-2d lst w h i j)
(list-ref lst (+ (* i w) j)))
; reflect-list-2d-v : (-> lst int int)
; Given a list which *represents* a 2-d w-by-h array,
; return that 2-d array with each row reflected vertically
; (mirror-imaged).
;
(define (reflect-list-2d-v data w h)
(build-list (* w h)
(lambda (i)
(let* {[row (quotient i w)]
[column (remainder i w)]
}
(list-ref-2d data w h row (- w column 1))))))
|
1
Optional problems are things which I expect you to be able to do,
and will be needed for future work,
but aren't scored for this homework.
(I will provide feedback on them, though.)
↩
2You are not being
asked to make one airplane object and then change its fields --
we're making three different airplane structs which are all similar
to each other.
↩
home—info—lectures—exams—archive