Struct 在scheme中用一个字段定义结构

Struct 在scheme中用一个字段定义结构,struct,scheme,Struct,Scheme,我正在为一个班做家庭作业。问题陈述说要使用数据定义: (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 di

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

(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博士(中级学生语言)说:


这里有我遗漏的东西吗?或者我的老师给了我一个无效的数据定义吗?

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

(define-struct diff-exp (exprs))

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

您可以快速浏览
define struct

的各种功能,我想您应该将
exprs
定义为一个元素的列表。这是完全正确的,除了说“字段列表”这句话,因为没有涉及列表,所以很容易混淆。只有语法看起来像(field1 field2…)。这是真的,我对此避而不谈,因为我不知道海报是否理解宏和函数之间的区别,但如果
exprs
实际上绑定到他的代码中的列表,这可能与他的问题有关。我的老师最终给了我回信,说这是他的错别字。
(define-struct diff-exp (exprs))

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