为什么我会收到此 body 错误?
Why am I getting this body error?
每次我 运行 这段代码,我都会收到错误消息: "define: expected only one expression for the function body, but found 1 extra part." 我已经一次又一次地尝试解决这个问题,但我还没有找到解决方案。有谁知道我该如何解决它?抱歉,代码太长了,我想我应该把它全部包括在内,否则就没有意义了。谢谢!
(define MT (empty-scene 50 50))
; A Polygon is one of:
; – (list Posn Posn Posn)
; – (cons Posn Polygon)
; A NELoP is one of:
; – (cons Posn empty)
; – (cons Posn NELoP)
; Polygon -> Image
; adds an image of p to MT
(define (render-polygon p)
(local
[;Polygon -> Posn
; extracts the last item from p
(define (last p)
(cond
[(empty? (rest (rest (rest p)))) (third p)]
[else (last (rest p))]))]
[;Image Posn Posn -> Image
(define (render-line im p q)
(add-line
im (posn-x p) (posn-y p) (posn-x q) (posn-y q) "red"))]
[;NELop -> Image
;connects the posns in p in an image
(define (connect-dots p)
(cond
[(empty? (rest p)) MT]
[else
(render-line
(connect-dots (rest p)) (first p) (second p))]))])
(render-line (connect-dots p) (first p) (last p)))
新代码(仍然无效):
; Polygon -> Image
; adds an image of p to MT
(define (render-polygon p)
(local
[;Polygon -> Posn
; extracts the last item from p
(define (last p)
(cond
[(empty? (rest (rest (rest p)))) (third p)]
[else (last (rest p))]))
;Image Posn Posn -> Image
(define (render-line im p q)
(add-line
im (posn-x p) (posn-y p) (posn-x q) (posn-y q) "red"))
;NELop -> Image
;connects the posns in p in an image
(define (connect-dots p)
(cond
[(empty? (rest p)) MT]
[else
(render-line
(connect-dots (rest p)) (first p) (second p))]))
(render-line (connect-dots p) (first p) (last p))]))
您的 render-line
表达式必须在 内部 local
形式,而不是在它之后。此外,您所有的 define
都应该在 local
的 一个 子表单中,而不是每个都在其自己的子表单中。所以,它应该看起来像:
(local [(define (last p)
...)
(define (render-line im p q)
...)
(define (connect-dots p)
...)]
(render-line ...))
每次我 运行 这段代码,我都会收到错误消息: "define: expected only one expression for the function body, but found 1 extra part." 我已经一次又一次地尝试解决这个问题,但我还没有找到解决方案。有谁知道我该如何解决它?抱歉,代码太长了,我想我应该把它全部包括在内,否则就没有意义了。谢谢!
(define MT (empty-scene 50 50))
; A Polygon is one of:
; – (list Posn Posn Posn)
; – (cons Posn Polygon)
; A NELoP is one of:
; – (cons Posn empty)
; – (cons Posn NELoP)
; Polygon -> Image
; adds an image of p to MT
(define (render-polygon p)
(local
[;Polygon -> Posn
; extracts the last item from p
(define (last p)
(cond
[(empty? (rest (rest (rest p)))) (third p)]
[else (last (rest p))]))]
[;Image Posn Posn -> Image
(define (render-line im p q)
(add-line
im (posn-x p) (posn-y p) (posn-x q) (posn-y q) "red"))]
[;NELop -> Image
;connects the posns in p in an image
(define (connect-dots p)
(cond
[(empty? (rest p)) MT]
[else
(render-line
(connect-dots (rest p)) (first p) (second p))]))])
(render-line (connect-dots p) (first p) (last p)))
新代码(仍然无效):
; Polygon -> Image
; adds an image of p to MT
(define (render-polygon p)
(local
[;Polygon -> Posn
; extracts the last item from p
(define (last p)
(cond
[(empty? (rest (rest (rest p)))) (third p)]
[else (last (rest p))]))
;Image Posn Posn -> Image
(define (render-line im p q)
(add-line
im (posn-x p) (posn-y p) (posn-x q) (posn-y q) "red"))
;NELop -> Image
;connects the posns in p in an image
(define (connect-dots p)
(cond
[(empty? (rest p)) MT]
[else
(render-line
(connect-dots (rest p)) (first p) (second p))]))
(render-line (connect-dots p) (first p) (last p))]))
您的 render-line
表达式必须在 内部 local
形式,而不是在它之后。此外,您所有的 define
都应该在 local
的 一个 子表单中,而不是每个都在其自己的子表单中。所以,它应该看起来像:
(local [(define (last p)
...)
(define (render-line im p q)
...)
(define (connect-dots p)
...)]
(render-line ...))