2010-02-11 71 views
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) 

然而,當我剛剛在源,計劃博士(中級學員語言)說:

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

有什麼事我在這裏失蹤還是我的老師給我一個無效的數據定義?

+1

我懷疑你應該將'exprs'定義爲一個元素的列表。 – 2010-02-11 01:38:35

回答

1

像上面評論中提到的Anon,define-struct需要一個字段列表;如果你只需要一個,那麼使用一個元素的列表。示例代碼:

(define-struct diff-exp (exprs)) 

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

您可以通過define-structin the PLT Scheme documentation許多各式各樣的功能步槍。

+0

這一切都是正確的,除了說「字段列表」這是因爲沒有列表涉及混淆。只有看起來像(field1 field2 ...)的語法。 – 2010-02-11 02:03:03

+0

這是真的,我掩飾了它,因爲我不知道海報是否理解宏和函數之間的區別,但是如果'exprs'實際上綁定到他的代碼中的列表,它可能實際上與他的問題有關。希望他儘管找到了我的路。 – mquander 2010-02-11 02:17:31

+0

我的老師最終回到了我的身邊,並說這是他的一個錯字。 – unholysampler 2010-02-11 13:09:13

相關問題