关于球拍:Chez计划记录-功能复制/更新?

Chez Scheme Record - Functional Copy/Update?

我已经在Chez Scheme文档中搜索了此问题的答案,但似乎找不到它:

Chez的记录是否具有功能性的副本/更新-类似Racket的结构?

谢谢。


支持功能更新的记录在SRFI 57中指定。实现由其原始作者Andr ??? van Tonder,作为可移植的R7RS库。可以在Chez中容纳R6RS的简单package器(包含R6RS)在野外使用,但是我将推荐一个综合系统,该系统可以出色地"大力摇晃代码直到其表现正常",即Akku.scm。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;; 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 ...)))) ...))))))