简体   繁体   English

用方案中的一个字段定义结构

[英]Define struct with one field in scheme

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: 但是,当我在源头中只有那个内容时,Scheme博士(中级学生语言)说:

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? 我在这里缺少什么吗?还是老师给了我一个无效的数据定义?

Like Anon suggested in the comment above, define-struct takes a list of fields; 就像上面评论中建议的Anon一样, define-struct接受一个字段列表; 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 . 您可以浏览PLT Scheme文档中define-struct的许多杂项功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM