程序代写代做代考 data structure Assignment 5: Recursion

Assignment 5: Recursion

Due Date: Thursday Febrary 9’th, 2017

Important Note Whenever we say to “design a function”, we mean that you need to follow the design recipe.
Any other time that you write a function in this class, you also need to follow the design recipe.

1 Many Numbers, Many Shapes

Exercise 1 Develop a data definition ManyNumbers which can store arbitrarily many numbers. Note, you have
to develop your own data definition, you may only use built-in primitive types and you may not use any
existing built-in data structures, you have to design your own data structures.

Exercise 2 Develop a data and structure definition for storing pairs of numbers representing x and y
coordinates.

Exercise 3 Develop a data definition ManyPairs which can store arbitrarily many pairs of numbers using the
data definition from the previous exercise.

Exercise 4 Write the templates for all of the three data definitions you have defined.

Exercise 5 Design a function, which, given ManyPairs, draws all of the points as circles with radius 10 on the
same empty scene, producing the resulting image.

Exercise 6 Design a function, which, given ManyPairs and two numbers for x and y coordinates, creates a
larger ManyPairs with all of the coordinates plus the new pair of coordinates.

Exercise 7 Using the functions you developed in exercise 5 and 6, design a big-bang program which places
a circle on every mouse event at the location of the mouse event.

2 Stars and Planets

Exercise 8 Develop a structure definition sun which can store a sun. Note that a sun is located at the center of
a solar system, so the distance of the sun to the center of the solar system is zero. A sun is similar to the solid
doll that we discussed in class.

Exercise 9 Develop a structure definition planet for storing a planet. A planet should contain a distance to
next inner planet, and should also contain the next inner most planet. Much like we defined a hollow doll in
class, a planet’s inner planet is like the inner nested doll in a hollow doll. This time however, an inner planet
may be either another planet, or it may be a sun.

Exercise 10 Develop a data definition solar-object which is the union of a planet and a sun. This will be
a union, i.e. a solar-object is one of …

Exercise 11 Write the templates for all of the three data definitions you have defined, i.e. process-sun,
process-planet, process-solar-object.

Exercise 12 Design a function which accpepts an solar-object and calculates and returns the distance of
the outermost solar object to the center of the solar system. I.e. the distance of the sun should be zero. Say we
have have a system of 3 objects, with a sun, planet1 with a distance of 10, and planet 2 with a distance of 20,
the distance of the system (planet 2) to the center should be 30.

Exercise 13 Design a function which accepts a solar object, and a distance, and returns a new solar object
which has the given solar object plus a new planet that is the given distance away from its inner object. The
give solar object should be the inner object of a new planet.

Exercise 14 Design a function which draws a sun. This should be a simple function which just draws a small
size 20 circle at the middle.

Exercise 15 Design a function which draws a solar object. Each planet in the solar object should be drawn as a
circle with a radius that you calculate as the distance of the planet to the center of the solar system. So, a
system of 3 objects, with two planets and a sun should appear as a single solid circle in the middle, sorrounded
by two rings, with the radius of each ring being the planets distance to the center.

The overlay function will be very useful here. Remember, the overlay function stacks all of its arguments
building a single image. The first argument goes on top of the second argument, and the images are all lined up
on their centers. For example:

(overlay (circle 20 “solid” “blue”) (circle 30 “outline” “red”))

will produce an image where a small solid blue circle is stacked on top of a larger hollow red ring. This could
be an example of drawing a solar object with one planet and one sun.

http://docs.racket-lang.org/teachpack/2htdpuniverse.html#%28form._world._%28%28lib._2htdp%2Funiverse..rkt%29._big-bang%29%29
http://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29
http://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29
http://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._overlay%29%29
http://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29
http://docs.racket-lang.org/teachpack/2htdpimage.html#%28def._%28%28lib._2htdp%2Fimage..rkt%29._circle%29%29