Function definition and function call input seem coherent, but there's an error about the input when the application runs. What's wrong?

huangapple go评论56阅读模式
英文:

Function definition and function call input seem coherent, but there's an error about the input when the application runs. What's wrong?

问题

It seems like you're encountering an issue where the render-cat function is receiving the entire cat struct instead of just the x field when called within the render-all function.

Based on your code, it looks like you're passing the correct x field to render-cat when calling it from render-all. The issue might be elsewhere in your code, possibly in how you're calling render-all or how you're constructing and updating the cat struct.

I recommend checking the following:

  1. Ensure that you're not inadvertently passing the entire cat struct to render-cat from any other part of your code.

  2. Double-check the implementation of update-all and other functions involved in updating the cat struct to make sure they are modifying the x field correctly.

  3. If possible, use debugging tools or print statements to trace the flow of your program and see where the cat struct might be getting passed incorrectly to render-cat.

Without access to the full code and execution environment, it's challenging to pinpoint the exact issue, but I hope these suggestions help you identify and resolve the problem.

英文:

A novice in programming here (also first time posting here), trying to solve ex.91 from the htdp book (in racket - beginner student language). There's trouble with a helper function, which I named 'render-cat'. Here it is:

; Position -> Image
; given pos, renders cat image on the canvas
(check-expect (render-cat 0)
              (place-image cat1 0 FLOOR BACKGROUND))
(check-expect (render-cat 50)
              (place-image cat1 50 FLOOR BACKGROUND))
(check-expect (render-cat (cat-x (make-cat 83 100 "right")))
              (place-image cat2 83 FLOOR BACKGROUND))

(define (render-cat pos)
  (cond
    [(odd? pos) (place-image cat2 pos FLOOR BACKGROUND)]
    [else       (place-image cat1 pos FLOOR BACKGROUND)]))

So, render-cat is supposed to consume a number (named Position here). Position is also the first field of the cat struct (named x):

(define-struct cat [x h dir])
; an example
(define vcat0 (make-cat 0 100 "right")

and here's the function that calls render-cat:

; VCat -> Image
; draws cat and happiness gauge
(check-expect (render-all vcat0)
              (overlay/align "right" "top"
                             (show-gauge (* 2 (cat-h vcat0)))
                             (render-cat (cat-x vcat0)))) ; merely selecting x
(define (render-all vc)
  (overlay/align "right" "top"
                 (show-gauge (* 2 (cat-h vc)))
                 (render-cat (cat-x vc))))

so, when render-cat is called by render-all, only the x field is passed to render-cat, basically a number - and the tests seem to agree. However, when I run the full world application - defined as such:

; VCat -> VCat
(define (happy-cat vc)
  (big-bang vc
    [on-tick update-all]
    [on-key pet]
    [to-draw render-all] ; the problem seems to be here
    [stop-when unhappy?])), 
  • this error pops up at once:
    odd?: expects an integer, given (make-cat 3 100 "right")
    ( (odd? pos) being the only thing highlighted, in render-cat)
    To my understanding this means that instead of just 3, the function somehow gets the entire struct in full, even though I'm explicitly passing only the x field value, and this happens in the next state of the world program (since the initial x of cat is 0 and the next is supposed to be 3). I have no idea how that happens, though. Cats are naughty and go where they want, even in the coding world I guess, even though they may not be more chaotic than my state of confusion right now.

I tried flooring the pos in the (odd? pos) part, just in case there was some issue there causing havoc, but the error was not about that, so, as I kind of expected, the same error occurred regardless.
I considered it might have to do with the data used, so I tried to have the function take the full VCat as input and extract the position field in its body instead, also changed the function call, passing in vc instead of (cat-x vc). Maybe this way the program would be more consistent and there wouldn't be any monkey business in between.

; VCat -> Image
; draws cat and happiness gauge
(check-expect (render-all vcat0)
              (overlay/align "right" "top"
                             (show-gauge (* 2 (cat-h vcat0)))
                             (render-cat vcat0)))
(define (render-all vc)
  (overlay/align "right" "top"
                 (show-gauge (* 2 (cat-h vc)))
                 (render-cat vc))) ;changed this

; VCat (changed this) -> Image
; given vc, renders cat image on the canvas
(check-expect (render-cat vcat0)
              (place-image cat1 0 FLOOR BACKGROUND))
(check-expect (render-cat vcat1)
              (place-image cat1 (cat-x vcat1) FLOOR BACKGROUND))
(check-expect (render-cat (make-cat 83 100 "right"))
              (place-image cat2 83 FLOOR BACKGROUND))

(define (render-cat vc)
  (cond
    [(odd? (cat-x vc)) (place-image cat2 (cat-x vc) FLOOR BACKGROUND)] ; changes also in the
    [else              (place-image cat1 (cat-x vc) FLOOR BACKGROUND)])); function definition

However, the same thing happens, the exact same error (all tests still pass). There's no other place in this world program where I'm using this function and the error is rather vague as to when the problem occurs exactly, only hinting that one update is made before the program stops (using the stepper didn't help much either), so at this point I'm lost.
If you could provide any insight on what the case might actually be, I'd greatly appreciate it.

答案1

得分: 0

根据Shawn和MartinPůda的建议,我查看了update-all,该函数正在更新VCat的世界状态。

; VCat -> VCat
; 更新x坐标、幸福度水平和方向
(check-expect (update-all vcat0)
(make-cat (update+ vcat0)
(lvl-down (cat-h vcat0))
(set-direction vcat0)))

(define (update-all vc)
(make-cat (update+ vc)
(lvl-down (cat-h vc))
(set-direction vc)))

原来我在使用一个辅助函数,该函数返回的是一个VCat而不是一个Position(update+),因此下一个VCat类似于(make-cat x h dir),其中x是(make-cat ...),因此在render-cat需要处理x时出现错误,而不是一个简单的数字。
(update+是我修改另一个函数而得到的。之前的版本已正确返回了一个Position,所以我将那部分改回来,现在它可以正常工作了!问题实质上出在辅助函数的混淆签名上:VCat -> VCat,它应该保持为VCat -> Pos。

英文:

As per Shawn and MartinPůda's suggestions, I looked into update-all which was updating the VCat world state.

; VCat -> VCat
; updates x coordinate, happiness level and direction
(check-expect (update-all vcat0)
              (make-cat (update+ vcat0)
                        (lvl-down (cat-h vcat0))
                        (set-direction vcat0)))

(define (update-all vc)
  (make-cat (update+ vc)
            (lvl-down (cat-h vc))
            (set-direction vc)))

Turns out I was using a helper function which was returning a VCat instead of a Position (update+), so the next VCat was something like (make-cat x h dir) where x was (make-cat ...), hence the error when render-cat had to work with x, now a cat struct rather than a simple number.
(update+ was a function I'd tweaked another one into. The previous version had been properly returning a Position, so I changed that part back and it works! The trouble was essentially with the confused signature of the helper function for the update: VCat -> VCat, where it should've remained VCat -> Pos.

huangapple
  • 本文由 发表于 2023年6月8日 04:09:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定