在方案中定义具有一个字段的结构体

发布于 2024-08-21 05:43:59 字数 647 浏览 6 评论 0原文

我正在为班级做家庭作业。问题陈述说要使用数据定义:

(define-struct diff-exp exprs)
(define-struct mult-exp exprs)
;; An Expr is one of
;; -- Number
;; -- (make-diff-exp (cons Expr LOExpr))
;; -- (make-mult-exp (cons Expr LOExpr))
;; Interpretation: a diff-exp represents a difference,
;; and a mult-exp represents a multiplication.
;; A List of Exprs (LOExpr) is one of
;; -- empty
;; -- (cons Expr LOExpr)

但是,当我在源中只有该数据定义时,Scheme 博士(中级学生语言)说:

define-struct: expected a sequence of field names after the structure type name in `define-struct', but found something else

我在这里遗漏了什么,还是我的老师给了我一个无效的数据定义?

I am working on a homework assignment for a class. The problem statement says to use the data definition:

(define-struct diff-exp exprs)
(define-struct mult-exp exprs)
;; An Expr is one of
;; -- Number
;; -- (make-diff-exp (cons Expr LOExpr))
;; -- (make-mult-exp (cons Expr LOExpr))
;; Interpretation: a diff-exp represents a difference,
;; and a mult-exp represents a multiplication.
;; A List of Exprs (LOExpr) is one of
;; -- empty
;; -- (cons Expr LOExpr)

However, when I have just that in the source, Dr. Scheme (Intermediate Student Language) says:

define-struct: expected a sequence of field names after the structure type name in `define-struct', but found something else

Is there something I am missing here or did my teacher give me an invalid data definition?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-08-28 05:43:59

就像 Anon 在上面的评论中建议的那样,define-struct 接受一个字段列表;如果您只需要一个,则使用一个元素的列表。示例代码:

(define-struct diff-exp (exprs))

(let ((myexpr (make-diff-exp (list foo bar))))
  (diff-exp-exprs myexpr))

您可以浏览 define-struct 的许多杂项功能 PLT 方案文档

Like Anon suggested in the comment above, define-struct takes a list of fields; if you need only one, then use a list of one element. Example code:

(define-struct diff-exp (exprs))

(let ((myexpr (make-diff-exp (list foo bar))))
  (diff-exp-exprs myexpr))

You can rifle through the many sundry features of define-struct in the PLT Scheme documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文