Scheme 方案使用位置速度方向更新子弹位置列表
我正在尝试根据子弹的方向和速度更新每个子弹的位置,以获得一个子弹列表。并返回更新的项目符号列表 我认为我的代码在逻辑上工作,但scheme给了我错误代码Scheme 方案使用位置速度方向更新子弹位置列表,scheme,racket,Scheme,Racket,我正在尝试根据子弹的方向和速度更新每个子弹的位置,以获得一个子弹列表。并返回更新的项目符号列表 我认为我的代码在逻辑上工作,但scheme给了我错误代码 function call: expected a function after the open parenthesis, but received (make-signature ...) ;; A Bullet is (make-bullet Posn Number Direction Number) ;; interpetatio
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))]))
哼。这里有什么问题吗 您将一个项目符号作为参数传递,因此需要提取其每个组件。试试这个:
(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)))]))
测试:
(移动项目符号(列表(make bullet(make posn 10 10)4'向上10))
它给出了项目符号方向:需要1个参数,但没有发现任何参数?在创建新项目符号中有一个错误,请再次复制并重试。我想我可以把makeposn
改成makebullet
@pnixsweet这就是你的意思,注意你的createnewbullet
代码返回调用makeposn
的结果