2
\$\begingroup\$

I am using Dr.Racket, Intermediate Student with Lambda. I was wondering if there was any way I can simplify this code using any sort of method like lambda, abstraction, map, filter, etc.

 ; DRAWING FUNCTIONS

 ; draw-rocket: rocket scene --> scene
 ; Purpose: To draw the given rocket in the given scene
 (define (draw-rocket a-rocket a-scene) 
 (place-image rocket-img a-rocket ROCKET-Y a-scene))


 ; draw-alien: alien scene --> scene
 ; Purpose: To draw the given alien in the given scene
 (define (draw-alien an-alien a-scene)
 (place-image alien-img 
   (posn-x an-alien) 
   (posn-y an-alien)
   a-scene))

    ; draw-aliens: loa scn --> scene 
    ; Purpose: To draw the aliens in the given scene 
    (define (draw-aliens a-loa scn) 
    (cond [(empty? a-loa) scn] 
    [else (draw-alien (first a-loa) (draw-aliens (rest a-loa) scn) 
                  )]))
;

; draw-shot: shot scene --> scene 
; Purpose: To draw the given shot in the given scene 
(define (draw-shot a-shot scn) 
(place-image SHOT-IMG (posn-x a-shot) (posn-y a-shot) scn))

; draw-aliens: loa scn --> scene 
; Purpose: To draw the aliens in the given scene 
(define (draw-shots a-los scn) 
(cond [(empty? a-los) scn] 
[else (draw-shot (first a-los) (draw-shots (rest a-los) scn) 
             )])) 

; draw-world: world --> scene
; Purpose: Draw the world in the empty scene
(define (draw-world a-world)
(draw-rocket (world-rocket a-world) 
(draw-aliens (world-aliens a-world)
\$\endgroup\$
  • \$\begingroup\$ Your draw-world has mismatched parentheses. \$\endgroup\$ – 200_success Dec 17 '13 at 9:33
  • \$\begingroup\$ In the future, please use better descriptive titles than "Is there anyway of simplifying this code?". I've edited both of the titles in your latest questions. \$\endgroup\$ – Simon Forsberg Dec 17 '13 at 13:14
2
\$\begingroup\$

There are significant issues here:

; draw-rocket: rocket scene --> scene
; Purpose: To draw the given rocket in the given scene
(define (draw-rocket a-rocket a-scene) 
(place-image rocket-img a-rocket ROCKET-Y a-scene))

Why break the pattern from drawing code for the others? Use (pos-x a-rocket), (pos-y a-rocket) instead of a-rocket ROCKET-Y. That will help you abstract the code later on.

; draw-aliens: loa scn --> scene 
; Purpose: To draw the aliens in the given scene 
(define (draw-shots a-los scn) 
(cond [(empty? a-los) scn] 
[else (draw-shot (first a-los) (draw-shots (rest a-los) scn) 
         )])) 

This method has the wrong purpose and signature. This should be:

; draw_shots: los scn --> scn
; Purpose: To draw the shots in the given scene

This code seems to have multiple issues:

; draw-world: world --> scene
; Purpose: Draw the world in the empty scene
(define (draw-world a-world)
(draw-rocket (world-rocket a-world) 
(draw-aliens (world-aliens a-world)
  1. The parentheses are mismatched, as mentioned in the comments.
  2. Why are the draw-shots methods not called here?

I also think that you want to nest the calls so that the results are cumulative. This is done like so:

; draw-world: world --> scene
; Purpose: Draw the world in the scene (why would does it have to be empty?)
(define (draw-world a-world)
(draw-shots (world-shots 
(draw-rocket (world-rocket 
(draw-aliens (world-aliens a-world)))))))

As for using higher-order functions:

Try using foldr like so:

(define (draw-shots a-los scn)
(foldr draw-shot (first a-los) (rest a-los)))

Similarly, you can apply this to all the functions on lists.


Finally, using abstraction is very easy here. Create a new type, called Sprite or Actor or something similar, and have Aliens, Shots and Rocket all extend it. That way you only need 2 functions: 1 for singular, 1 for plural.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.