使用球拍中的图像模块的递归问题

问题描述 投票:0回答:1

我在第27行出现以下错误:

first: expects a non-empty list; given: #<image>

递归有问题吗?

我一直在研究,但是找不到递归使用图像包的示例。

我什么也没想,我不明白为什么它会给我这个错误:(

; You must make a program that needs a list of n figures, which you will paint,
; all the color options that the user indicates and separates by a fixed distance that you determine.
; These figures contain a name (they depend on the naming options that you set),
; a measurement or measurements according to what the figure requires and an indicator of whether it is painted full or only the silhouette.
; NOTE: These drawings must be made using the image of the package.


(require 2htdp/image)

(define-struct triangulo_ (color size complete))
(define-struct cuadrado_  (color size complete))
(define-struct circulo_   (color size complete))

(define circulo   (make-circulo_   "yellow" 10 "outline"))
(define triangulo (make-triangulo_ "blue"   10 "solid"))
(define cuadrado  (make-cuadrado_  "orange" 10 "outline"))

(define lista_figuras (cons circulo (cons triangulo (cons cuadrado empty))))

(define (validar figura ID)
  (cond
    [(empty? figura) ID]
    [(and (circulo_? (first figura))   (positive? (circulo_-size (first figura))) (number? (circulo_-size (first figura)))   (string? (circulo_-color (first figura)))   (string? (circulo_-complete (first figura))))   (validar (rest figura) #t)]
    [(and (cuadrado_? (first figura))  (positive? (cuadrado_-size (first figura))) (number? (cuadrado_-size (first figura)))  (string? (cuadrado_-color (first figura)))  (string? (cuadrado_-complete (first figura))))  (validar (rest figura) #t)]
    [(and (triangulo_? (first figura)) (positive? (triangulo_-size (first figura))) (number? (triangulo_-size (first figura))) (string? (triangulo_-color (first figura))) (string? (triangulo_-complete (first figura)))) (validar (rest figura) #t)]
    [else (validar (rest figura) #f)]))

(define (pathway figura)
  (cond
    [(empty? figura) empty]
    [(circulo_?   (first figura)) (pathway(circle   (circulo_-size (first figura))   (circulo_-complete (first figura))   (circulo_-color (first figura))))]
    [(triangulo_? (first figura)) (pathway(triangle (triangulo_-size (first figura)) (triangulo_-complete (first figura)) (triangulo_-color (first figura))))]
    [(cuadrado_?  (first figura)) (pathway(square   (cuadrado_-size (first figura))  (cuadrado_-complete (first figura))  (cuadrado_-color (first figura))))]
    [else (pathway (rest figura))]))

(define (main figura)
  (cond
    [(boolean=? (validar figura 0) #t) (pathway figura)]
    [else "Error, solo se permiten: [ circulos, cuadrados y triangulos <equilateros> ]"]))

(main lista_figuras)
racket
1个回答
0
投票

这是类型错误。在对pathway的递归调用中,您正在传递图像,但是它需要一个包含triangulo_ / cuadrado_ / circulo_的列表。同样,validar中cond子句的右侧有多个表达式,每个表达式都将从左到右求值,但是即使它们是false,它们的值也无关紧要。最好将每个字段的验证与and结合使用。

首先,给您定义的结构起个名字,我们称它为ImageConfig

在较高的层次上,设计的问题是,您将列表处理与列表中元素的处理混为一谈。最好将它们分开。

现在将[List-of ImageConfig]ImageConfigvalidarpathway的处理分开。

; validar-l : [List-of ImageConfig] -> Boolean
; validar   : ImageConfig -> Boolean
; pathway-l : [List-of ImageConfig] -> [List-of Image]
; pathway   : ImageConfig -> Image
; main      : [List-of ImageConfig] -> [List-of Image]

; You must make a program that needs a list of n figures, which you will paint,
; all the color options that the user indicates and separates by a fixed distance that you determine.
; These figures contain a name (they depend on the naming options that you set),
; a measurement or measurements according to what the figure requires and an indicator of whether it is painted full or only the silhouette.
; NOTE: These drawings must be made using the image of the package.


(require 2htdp/image)

; ImageConfig is one of
(define-struct triangulo_ (color size complete))
(define-struct cuadrado_  (color size complete))
(define-struct circulo_   (color size complete))

(define circulo   (make-circulo_   "yellow" 10 "outline"))
(define triangulo (make-triangulo_ "blue"   10 "solid"))
(define cuadrado  (make-cuadrado_  "orange" 10 "outline"))

(define lista_figuras (cons circulo (cons triangulo (cons cuadrado empty))))

; [List-of ImageConfig] -> Boolean
(define (validar-l fl)
  (cond [(empty? fl) true]
        [else (and (validar (first fl)) (validar-l (rest fl)))]))

; ImageConfig -> Boolean
(define (validar f)
  (cond
    [(circulo_? f)   (and (positive? (circulo_-size f))   (number? (circulo_-size f))   (string? (circulo_-color f)))]
    [(cuadrado_? f)  (and (positive? (cuadrado_-size f))  (number? (cuadrado_-size f))  (string? (cuadrado_-color f)))]
    [(triangulo_? f) (and (positive? (triangulo_-size f)) (number? (triangulo_-size f)) (string? (triangulo_-color f)))]))


; ImageConfig -> Image
(define (pathway f)
  (cond
    [(circulo_?   f) (circle   (circulo_-size f)   (circulo_-complete f)   (circulo_-color f))]
    [(triangulo_? f) (triangle (triangulo_-size f) (triangulo_-complete f) (triangulo_-color f))]
    [(cuadrado_?  f) (square   (cuadrado_-size f)  (cuadrado_-complete f)  (cuadrado_-color f))]))

; [List-of ImageConfig] -> [List-of Image]
(define (pathway-l fl)
  (cond
    [(empty? fl) empty]
    [else (cons (pathway (first fl)) (pathway-l (rest fl)))]))

; [List-of ImageConfig] -> [List-of Image]
(define (main fl)
  (cond
    [(boolean=? (validar-l fl) true) (pathway-l fl)]
    [else (error "Error, solo se permiten: [ circulos, cuadrados y triangulos <equilateros> ]")]))

(main lista_figuras)

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.