Chez Scheme 记录 - 功能 Copy/Update?
Chez Scheme Record - Functional Copy/Update?
我在 Chez Scheme 文档中搜索了这个问题的答案,但似乎找不到:
Chez 的 Records - something like Racket has for its Structures 有功能性的 copy/update 吗?
谢谢。
SRFI 57. An implementation is provided by its original author, André van Tonder, as a portable R7RS library. Simple wrappers that can accommodate R7RS in Chez, which embraces R6RS, are available in the wild, but I will recommend a comprehensive system that does an excellent job of "vigorously shaking code it until it behaves properly", namely Akku.scm 中指定了支持功能更新的记录。
.
我最近编写了一个名为 define-immutable-record
的宏,它生成一个不可变的记录类型以及如下命名的过程:record-with-field
其中 return 更新了记录的副本。这是:
;; 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 ...)))) ...))))))
我在 Chez Scheme 文档中搜索了这个问题的答案,但似乎找不到:
Chez 的 Records - something like Racket has for its Structures 有功能性的 copy/update 吗?
谢谢。
SRFI 57. An implementation is provided by its original author, André van Tonder, as a portable R7RS library. Simple wrappers that can accommodate R7RS in Chez, which embraces R6RS, are available in the wild, but I will recommend a comprehensive system that does an excellent job of "vigorously shaking code it until it behaves properly", namely Akku.scm 中指定了支持功能更新的记录。
.
我最近编写了一个名为 define-immutable-record
的宏,它生成一个不可变的记录类型以及如下命名的过程:record-with-field
其中 return 更新了记录的副本。这是:
;; 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 ...)))) ...))))))