Why can't I make two function calls in a function body?

So, I will review the first chapter, "How to Develop Programs for the Second Edition." I believe that I have achieved good success. But there is a β€œsuggestion” to add another graphic to the grid. Every time I try, I get an error message. At this moment I am stuck. Below is the code and error.

Note: the ROCKET image is in chapter 1. I just copied and pasted it into the IDE.

Note. "Suggestion": How to change the program so that the rocket lands on a flat rock bed, which is 10 pixels above the bottom of the stage? Remember to also change the scenery.

HTDP Chapter 1

This is where the code works.

(define BOARDWIDTH 200) (define BOARDHEIGHT 200) (define STARTPOSITION 50) (define BOARDBKGR "blue") (define GAMEBOARD (empty-scene BOARDWIDTH BOARDHEIGHT BOARDBKGR)) (define ROCKET .) (define UFO (overlay (circle 10 "solid" "red") (rectangle 40 4 "solid" "green"))) (define FLATBED (rectangle 60 10 "outline" "black")) (define (SPACESHIP option) (cond [(= option 1) ROCKET] [(= option 2) UFO])) (define SHOWNSHIP (SPACESHIP 1)) (define V 20) ;Velocity (define A 1) ;Acceleration (define (distance t) ;t = Time (- (* V t) (* 1/2 A (sqr t)))) (define SPACESHIP-BOTTOM (- BOARDHEIGHT (/ (image-height SHOWNSHIP) 2))) (define (render-shownship xy) (place-image SHOWNSHIP xy GAMEBOARD)) (define (create-rocket-scene.v7 t) (cond [(<= (distance t) SPACESHIP-BOTTOM) (render-shownship STARTPOSITION (distance t))] [(> (distance t) SPACESHIP-BOTTOM) (render-shownship STARTPOSITION SPACESHIP-BOTTOM)])) 

Here's the code that doesn't work:

 (define BOARDWIDTH 200) (define BOARDHEIGHT 200) (define STARTPOSITION 50) (define BOARDBKGR "blue") (define GAMEBOARD (empty-scene BOARDWIDTH BOARDHEIGHT BOARDBKGR)) (define ROCKET .) (define UFO (overlay (circle 10 "solid" "red") (rectangle 40 4 "solid" "green"))) (define FLATBED (rectangle 60 10 "outline" "black")) (define (SPACESHIP option) (cond [(= option 1) ROCKET] [(= option 2) UFO])) (define SHOWNSHIP (SPACESHIP 1)) (define V 20) ;Velocity (define A 1) ;Acceleration (define (distance t) ;t = Time (- (* V t) (* 1/2 A (sqr t)))) (define SPACESHIP-BOTTOM (- BOARDHEIGHT (/ (image-height SHOWNSHIP) 2))) (define (render-shownship xy) (place-image SHOWNSHIP xy GAMEBOARD) (place-image FLATBED STARTPOSITION 195 GAMEBOARD)) ;offender (define (create-rocket-scene.v7 t) (cond [(<= (distance t) SPACESHIP-BOTTOM) (render-shownship STARTPOSITION (distance t))] [(> (distance t) SPACESHIP-BOTTOM) (render-shownship STARTPOSITION SPACESHIP-BOTTOM)])) 

And I get the error:

define: only one expression is expected for the function body, but 1 additional part is found

+4
source share
1 answer

place-image always takes 4 arguments - the image to place, the x and y coordinates and the scene (background) on which you want to place the image. The problem in your code is that the expression (place-image FLATBED STARTPOSITION 195) provides only 3 inputs for place-image .

So, eat a little and think: what produces the first expression? (place-image SHOWNSHIP xy GAMEBOARD) creates a scene of a playing field with a ship on it, right? Now, on top of this scene, you'll also want to place FLATBED . Therefore, instead of arranging the calls to the place-image function, instead think about compiling them, i.e. What do you think the missing part is in (place-image FLATBED STARTPOSITION 195 ____) ? On which stage do you want to place FLATBED ? (Hint: we just answered above). What expression creates this scene? (hint: you already have this expression).

If you understand this idea, you see that to place multiple images on the stage, you create or insert function calls (instead of arranging them as you try):

 (place-image img1 x1 y1 (place-image img2 x2 y2 ...)) 
+4
source

Source: https://habr.com/ru/post/1389978/


All Articles