为什么我会收到这个正文错误


每次

运行此代码时,我都会收到错误消息:"定义:函数体只有一个表达式,但找到了 1 个额外的部分。我一次又一次地尝试解决这个问题,但我还没有找到解决方案。有人知道我该如何解决它吗?对不起,代码很长,我想我应该全部包含它,否则它就没有意义了。谢谢!

(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 ...))

最新更新