我无法在方案中定义一些代码。我正在尝试为Scheme / Racket中的节点创建记录,到目前为止,我的代码如下所示:
(define-record-type node
(define-record-type node
(make-node v l r)
(make-node v l r)
node?
node?
然而 - 当我尝试执行时,我收到以下错误:
(v tree-value)
我知道你可以定义字段类型是可变的,不可变的等等。但是我不明白为什么这需要是构造函数中的一个子句 - 如果是这样的话应该在哪里呢?
有人可以解释一下
1)如何解决此错误
2)为什么会出现
答案 0 :(得分:0)
您的代码与the R6RS standard reference doc不符,可能的定义是:
#!r6rs
(import (rnrs) (rnrs records syntactic))
(define-record-type node (fields v l r))
(define tree-value (record-accessor node 0))
(define tree-left (record-accessor node 1))
(define tree-right (record-accessor node 2))
或
#!r6rs
(import (rnrs) (rnrs records syntactic))
(define-record-type node
(fields
(immutable v tree-value)
(immutable l tree-left)
(immutable r tree-right)))
或
#!r6rs
(import (rnrs) (rnrs records syntactic))
(define-record-type (node make-node node?)
(fields
(immutable v tree-value)
(immutable l tree-left)
(immutable r tree-right)))
对于其中任何一个,你得到:
(define n (make-node 1 2 3))
(display n) (newline)
=> #(struct:node 1 2 3)
(display (node? n)) (newline)
=> #t
(display (tree-value n)) (newline)
=> 1
(display (tree-left n)) (newline)
=> 2
(display (tree-right n)) (newline)
=> 3