s c h e m a t i c s : c o o k b o o k

/ Cookbook.StructureFunctionalUpdates

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Functional Updates

Problem

You want to update fields without overwriting an existing structure.

Solution

The PLT Scheme standard libraries provide a functional update syntax form called copy-struct. See Chapter 46 of the mzscheme standard library manual.

Here's an example:

> (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)

Discussion

You can simplify this syntax a bit for each of your structure types with the following macro:

(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))]))

Now you can define a shortcut for copy-struct. Following from the example above,

> (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)

This also lets you continue the practice of naming structure instances the same as the structure name. With copy-struct this would fail:

> (define-struct s (a b))
> (define s (make-s 5 6))
> (copy-struct s s [s-a 9])
copy-struct: identifier is not bound to a structure type in: s

But there are no clashes when using update:

> (define-struct s (a b))
> (define s (make-s 5 6))
> (update s [a 9])
#(struct:s 9 6)


Comments about this recipe

Contributors

-- DanielSilva - 11 Jun 2007

CookbookForm
TopicType: Recipe
ParentTopic: StructureRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest