Struct 带另一个结构的方案结构

Struct 带另一个结构的方案结构,struct,scheme,racket,user-defined-types,Struct,Scheme,Racket,User Defined Types,我试图自学scheme(drracket),但遇到了一个问题 我正在尝试使用不同的形状,如圆形、正方形和矩形。 任务如下: 定义类型“圆形”、“方形”和“矩形”,并定义主要类型“形状”。 在任务的下一部分中,我需要shape结构,在下一部分中,我必须定义一个函数“area of shape”,该函数获取一个“shape”数据绑定,并且应该表示任何给定形状的区域 以下是我到目前为止的情况: (define-struct square (nw lenght)) (define-struct cir

我试图自学scheme(drracket),但遇到了一个问题

我正在尝试使用不同的形状,如圆形、正方形和矩形。 任务如下:

定义类型“圆形”、“方形”和“矩形”,并定义主要类型“形状”。 在任务的下一部分中,我需要shape结构,在下一部分中,我必须定义一个函数“area of shape”,该函数获取一个“shape”数据绑定,并且应该表示任何给定形状的区域

以下是我到目前为止的情况:

(define-struct square (nw lenght))

(define-struct circle (center radius))

(define-struct rectangle (nw width height))

(define-struct shape (...))



(define (area-of-shape shapeA)
  (cond 
    [(square? (shapeA)) (* (square-lenght shapeA) (square-lenght shapeA))]
    [(circle? (shapeA)) (* 3.14 (* (circle-radius shapeA) (circle-radius shapeA)))]
    [(rectangle? (shapeA)) (* (rectangle-width shapeA) (rectangle-height shapeA))]))
如何定义结构
形状

(定义结构形状(圆形-方形-矩形))

但这没有任何意义,因为结构需要所有3种形状


任何帮助都将不胜感激。

我将按以下方式进行:

(define (area-of-shape shapeA)
  (cond 
    [(square?    shapeA) (* (square-lenght shapeA) (square-lenght shapeA))]
    [(circle?    shapeA) (* 3.14 (* (circle-radius shapeA) (circle-radius shapeA)))]
    [(rectangle? shapeA) (* (rectangle-width shapeA) (rectangle-height shapeA))]
    [else (error 'area-of-shape "A shape is either a square, a circle, or, a rectangle")]))

球拍结构可以从另一个结构继承。因此:

#lang racket

(struct shape ())
(struct square shape (nw length))
(struct circle shape (center radius))
(struct rectangle shape (nw width height))

(define (area-of-shape shape)
  (if (shape? shape)
    (cond
      [(square? shape)    (* (square-length shape) (square-length shape))]
      [(circle? shape)    (* 3.14 (* (circle-radius shape) (circle-radius shape)))]
      [(rectangle? shape) (* (rectangle-width shape) (rectangle-height shape))]
      [else (error 'area-of-shape "Unhandled Shape Condition.")])
    (error 'area-of-shape "Argument not a Shape.")))

;; Example uses
(area-of-shape (square 0 10))
(area-of-shape (circle 0 10))
(area-of-shape (rectangle 0 10 10))
顺便说一下,对于形状的
区域
,我发现使用
匹配
比使用
条件
更方便:

(define (area-of-shape shape)
  (match shape
    [(square _ len)    (* len len)]
    [(circle _ rad)    (* 3.14 (* rad rad))]
    [(rectangle _ w h) (* w h)]))

你想要那个结构做什么?如果我理解正确的话。你可以将你定义为方圆或矩形的任何变量传递给你的函数。而且,你的参数
(shapeA)
。这应该会导致解释器出错。基本上我只希望形状区域工作(!)关于形状。不是任何其他数字或其他东西。所以我基本上想给函数和“形状”赋予一个“形状”应定义为正方形、圆形或矩形。这是我在网上发现的一个没有解决方案的练习。形状的功能区域未完成。我把它包括进来是为了说明它应该做什么。我可以添加任务使其更清晰,就像一个路标一样,
#lang racket
的结构是racket环境的基本构建块。使它们成为语言定义中的第一类是使
racket
不同于R5RS方案的一件大事。这一决定的重要性体现在其丰富的功能和相对较高的实现复杂度上。这也是我的想法,但据说shape的功能区域必须得到“shape objekt”的东西。我必须定义结构的形状。对不起,我的英语不好。你可以简单地说一个形状物体是正方形、圆形或矩形。非常感谢:)这正是我想要的。我以前没看过《火柴》,但我会查一下的。谢天谢地,我得到了以下错误:define struct:在结构名之后至少应该有一个字段名(在括号中),但发现了其他内容“因为在结构名之后,它需要一个字段名,但得到”shape请注意,我使用的是
struct
define struct
是一个旧的/旧的表单。所以它不使用define struct?我仍然在使用“lambda中级学生”的设置。所以它给了我一个错误,说“struct”没有定义。即使我更改了设置,它仍然会给我相同的错误。请参阅文档以了解更多信息。看起来您需要的是
id-maybe-super
的变体,即
(id-super-id)
。所以您需要
(定义结构(方形)(nw长度))
等等。