procgen  Changes To struct.scm

Initial version of "struct.scm"

            1  +<h1>functional structures in scheme</h1>
            2  +<code>struct.scm</code> contains a function to generate functional "structs." these structs are immutable and are implemented as lambdas. you can create a new "struct type" by calling the <code>(struct)</code> function. consider the following code:
            3  +
            4  +<pre><code>(define user (struct 'name          'pw               'rank           'email              'age 'attunements))
            5  +(define juan (user   "juán peralta" "p@labra-secr3ta" "special agent" "j.peralta@tla.gov" 69
            6  +                   '("hushed whispers" "the unconquered wilderness" "careful duplicity")))</code></pre>
            7  +
            8  +this code first creates a struct type, characterized by the function <code>(user)</code>. when <code>(user)</code> is called on the correct number of arguments, it generates a new record of that type. the new record is itself a function. to access a record, you simply call the function with the name of the field to look up. the library also supports what's known as "functional record update syntax," a pattern in which a new struct is returned with different values for named fields. this library currently only supports updating one record at a time, but you can get around this through currying.
            9  +
           10  +<pre><code>(juan) ; → '("juán peralta" "p@labra-secr3ta" "special agent" "j.peralta@tla.gov" 69
           11  +             '("hushed whispers" "the unconquered wilderness" "careful duplicity"))
           12  +
           13  +(juan 'email) ; → "j.peralta@tla.gov"
           14  +(juan 'attunements) ; → '("hushed whispers" "the unconquered wilderness" "careful duplicity")
           15  +
           16  +(define juan-with-new-pw (juan 'pw "pendej0-supremo"))
           17  +(juan-with-new-pw 'name) ; → "juán peralta"
           18  +(juan-with-new-pw 'pw) ; → "pendej0-supremo"
           19  +
           20  +(define clarabetta ((juan 'name "clarabetta kincaid") 'email "c.kincaid@tla.gov))
           21  +
           22  +(clarabetta 'email) ; → "c.kincaid@tla.gov"
           23  +
           24  +&nbsp;</code></pre>
           25  +
           26  +structs, as you can see above, can also be called without arguments to return a list of all the data they contain. the code is fairly straightforward, implemented completely without macros, and shouldn't be difficult to understand for anyone familiar with functional programming. it's also in my experience tremendously useful, simplifying a number of tasks that would otherwise be unnecessarily complicated in Scheme compared to C, and has the advantage of field names being expressions - you could just as easily call a function to return the appropriate field name as write one in yourself.