scheme更新具有位置速度方向的项目符号位置列表

时间:2014-11-06 20:02:14

标签: scheme racket

我正在尝试根据子弹列表的子弹方向和速度更新每个子弹的位置。并返回更新的项目符号列表。

我认为我的代码在逻辑上有效但方案给了我错误代码

function call: expected a function after the open parenthesis, 
but received (make-signature ...)


;; A Bullet is (make-bullet Posn Number Direction Number)
;; interpetation: represents a bullet, its current position, 
;;                its speed, its direction and its potential damage
(define-struct bullet (location speed direction damage))

;; A Direction is one of 
;; - 'Up
;; - 'Down
;; - 'Left 
;; - 'Right

;; a list of bullets(lob) is either
;; - empty 
;; - (cons bullet lob)

;; A location is Lof[location]
;; A location is a Posn

;; move-bullets: lob -> lob 
;;  takes in a list of bullets and updates each bullet's location
;;  based on the bullets direction and speed

(define (move-bullets lob)
  (cond 
     [(empty? lob) empty]
     [(cons? lob) (cons (create-new-bullet (first lob))
                        lob)]))

;; create-new-bullet: location direction speed-> location
;; takes in a list of bullets' location and create the "moved" bullet
;; base on given direction, speed

(define (create-new-bullet location dir speed)
  (cond 
    [(symbol=? 'Up dir) 
     (make-posn (posn-x location) (- (posn-y location) speed))] 
    [(symbol=? 'Down dir) 
     (make-posn (posn-x location) (+ (posn-y location) speed))]
    [(symbol=? 'Left dir)
     (make-posn (- (posn-x location) speed) (posn-y location))]
    [(symbol=? 'Right dir) 
     (make-posn (+ (posn-x location) speed) (posn-y location))]))

hummh。这里出了什么问题???

1 个答案:

答案 0 :(得分:2)

您已将项目符号作为参数传递,因此您需要提取其每个组件。试试这个:

(define (create-new-bullet bullet)
  (let ((location (bullet-location bullet))
        (speed (bullet-speed bullet))
        (direction (bullet-direction bullet))
        (damage (bullet-damage bullet)))
    (cond 
      [(symbol=? 'Up direction)
       (make-bullet
        (make-posn (posn-x location) (- (posn-y location) speed))
        speed direction damage)] 
      [(symbol=? 'Down direction) 
       (make-bullet
        (make-posn (posn-x location) (+ (posn-y location) speed))
        speed direction damage)]
      [(symbol=? 'Left direction)
       (make-bullet
        (make-posn (- (posn-x location) speed) (posn-y location))
        speed direction damage)]
      [(symbol=? 'Right direction) 
       (make-bullet
        (make-posn (+ (posn-x location) speed) (posn-y location))
        speed direction damage)])))

另外,你忘了推进递归:

(define (move-bullets lob)
  (cond 
    [(empty? lob) empty]
    [(cons? lob) (cons (create-new-bullet (first lob))
                       (move-bullets (rest lob)))]))