使用scheme中的一个字段定义struct

时间:2010-02-11 01:27:42

标签: 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 difference,
;; and a mult-exp represents a multiplication.
;; A List of Exprs (LOExpr) is one of
;; -- empty
;; -- (cons Expr LOExpr)

然而,当我在源头中有这样的时候,Dr. Scheme(中级学生语言)说:

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

我在这里缺少什么或者我的老师给我一个无效的数据定义吗?

1 个答案:

答案 0 :(得分:1)

就像上面评论中提到的Anon一样,define-struct采用了一系列字段;如果只需要一个,则使用一个元素的列表。示例代码:

(define-struct diff-exp (exprs))

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

您可以搜索define-struct in the PLT Scheme documentation的许多各种功能。