racketracket-student-languageshtdp

# New to programming, question about exercise in Prologue of HTDP

This may be a silly question but I'm completely new to programming. I'm towards the bottom of the Prologue in "How to Design Programs" (2nd ed) and there are 4 changes that you are challenged to make to the "Rocket Landing" program in DrRacket.

I'm stuck on the 4th one, specifically the bolded part:

How would you change the program so that the rocket lands on a flat rock bed that is 10 pixels higher than the bottom of the scene? Don’t forget to change the scenery, too.

``````; constants
(define WIDTH 200)
(define HEIGHT 400)
(define SCENE-CENTER (/ WIDTH 2))
(define MTSCN (empty-scene WIDTH HEIGHT "blue"))
(define ROCKET (overlay (circle 10 "solid" "green")
(rectangle 40 4 "solid" "green")))
(define ROCKET-CENTER-TO-TOP
(- (- HEIGHT 10) (/ (image-height ROCKET)2)))

;functions
(define (picture-of-rocket.v2 h)
(cond
[(<= h ROCKET-CENTER-TO-TOP)
(place-image ROCKET SCENE-CENTER h MTSCN)]
[(> h ROCKET-CENTER-TO-TOP)
(place-image ROCKET SCENE-CENTER ROCKET-CENTER-TO-TOP MTSCN)]))

(animate picture-of-rocket.v2)
``````

My rocket (ufo) is doing the first part, stopping 10 pixels from the bottom but I'm not sure how I can draw a rock bed at the bottom with the commands I've learned so far. Do I (overlay) a 10 pixel rectangle at the bottom? Or does the author just mean change the scene background to grey?

Solution

• Good question! I think, in this case, there is no right answer. Indeed, I think that is the very purpose of the exercise: to give you a problem to solve that requires a (very small) bit of creativity, without spelling out exactly what you ought to do. HtDP recognizes that programming requires a mixture of structured, principled process and open-ended critical thinking, and some of its questions are open-ended by design. This is not so much to test your creative skills as it is to make sure you can still apply what you’ve learned with the training wheels off.

Your first intuition—to add a rectangle at the bottom—is a good one. Again, there is no right answer, so if it works, for now, that’s okay. The intent is to get you to play around with the toolbox that’s been given to you. As for what it means to “change the scenery”, I interpret that as literally asking you to decorate the scene as you see fit.

If this open-endedness feels a little disorienting, don’t worry—that is, in large part, the point. If you read just a little further down, the final section of the prologue explains exactly why this kind of open-ended experimentation is not sufficient to become a skilled programmer. As you progress through HtDP and move beyond the prologue, you’ll be presented with a much more principled approach to program design than simply fiddling with things. For now, though, the important point of note is that you’re being thoughtful about what you’re doing, and that’s a good sign.

(By the way, this is one of the best Stack Overflow questions on anything related to HtDP I’ve seen in a long time, so thank you for that. Most are little more than thinly-veiled requests from students asking for people to do their homework for them.)