This Web
Other Webs
Schematics
Scheme Links
copy-struct
> (define-struct s (a b) (make-inspector)) > (define x (make-s 1 2)) > x #(struct:s 1 2) > (s-a x) 1 > (s-b x) 2 > (require (lib "struct.ss")) > (copy-struct s x [s-a 7]) #(struct:s 7 2)
(define-syntax (define-struct-update stx) (syntax-case stx () [(_ struct update) (and (identifier? #'struct) (identifier? #'update)) (with-syntax ([ellip '...]) #'(define-syntax (update stx) (syntax-case stx () [(_ s field-update ellip) (with-syntax ([(renamed-field-update ellip) (map (lambda (field-update-stx) (syntax-case field-update-stx () [(field v) (identifier? #'field) (with-syntax ([renamed-field (datum->syntax-object #'field (string->symbol (format "~a-~a" 'struct (syntax-e #'field))) #'field #'field #'field)]) #'(renamed-field v))])) (syntax->list #'(field-update ellip)))]) #'(copy-struct struct s renamed-field-update ellip))])))] [(define-struct-update struct) #`(define-struct-update struct #,(datum->syntax-object stx 'update stx stx stx))]))
> (define-struct-update s) > (update x [a 8]) #(struct:s 8 2) > (define-struct-update s with) > (with x [b 9]) #(struct:s 1 9)