1

我在 Chez Scheme 文档中搜索了这个问题的答案,但似乎找不到:

Chez 的Records是否有功能副本/更新- 就像 Racket 的Structures一样?

谢谢你。

4

2 回答 2

2

支持功能更新的记录在SRFI 57中指定。其原作者 André van Tonder 提供了一个实现,作为可移植的 R7RS 库。可以在包含 R6RS 的 Chez 中容纳 R7RS 的简单包装器在野外可用,但我会推荐一个综合系统,该系统在“大力摇动代码直到其正常运行”方面做得非常出色,即Akku.scm

.

于 2019-05-12T22:46:25.027 回答
1

我最近编写了一个名为的宏define-immutable-record,它产生一个不可变的记录类型以及如下命名的过程:record-with-field它返回记录的更新副本。这里是:

;; Synthesises a symbol from strings and syntax objects
(meta define (synth-symbol . parts)
  (define (part->string x)
    (if (string? x) x (symbol->string (syntax->datum x))))
  (string->symbol (apply string-append (map arg->string parts))))

;; Synthesises an identifier for use in an unhygenic macro
(meta define (gen-id template-id . parts)
  (datum->syntax template-id (apply synth-symbol parts)))

;; Defines a record with all fields being immutable. Also defines
;; functions of the form `record-with-field`, e.g:
;;
;; (define-immutable-record point (x 0) (y 1))
;; (make-point) => (point (x 0) (y 1))
;; (make-point 3) => (point (x 3) (y 1))
;; (point-with-x (make-point 3 4) 99) => (point (x 99) (y 4))
(define-syntax define-immutable-record
  (lambda (x)

    (define (gen-ids k record-name conj-str fields)
      (dat->syn k (map (lambda (f) (synth-symbol record-name conj-str f)) 
                       (syn->dat fields))))

    (syntax-case x ()
      ((k record-name field ...)
       (with-syntax ((make-fn (gen-id #'k "make-" #'record-name))
                     ((with-fn ...) (gen-ids #'k #'record-name "-with-" #'(field ...)))
                     ((get-fn ...)  (gen-ids #'k #'record-name "-" #'(field ...))))
         #'(begin
             (define-record-type record-name
               (fields (immutable field) ...))

             (define (with-fn record new-value)
               (let ([field (get-fn record)] ...)
                 (let ([field new-value])
                   (make-fn field ...)))) ...))))))
于 2020-01-13T09:43:48.013 回答