procgen  Artifact [d6e272adc9]

Artifact d6e272adc973afa804ae93733de1c5b168f742340b54dde21ea3477cec689ab8:

Wiki page [struct.scm] by lexi on 2019-03-30 05:59:13.
D 2019-03-30T05:59:13.199
L struct.scm
U lexi
W 2453
<h1>functional structures in scheme</h1>
<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:

<pre><code>(define user (struct 'name          'pw               'rank           'email              'age 'attunements))
(define juan (user   "juán peralta" "p@labra-secr3ta" "special agent" "j.peralta@tla.gov" 69
                   '("hushed whispers" "the unconquered wilderness" "careful duplicity")))</code></pre>

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.

<pre><code>(juan) ; → '("juán peralta" "p@labra-secr3ta" "special agent" "j.peralta@tla.gov" 69
             '("hushed whispers" "the unconquered wilderness" "careful duplicity"))

(juan 'email) ; → "j.peralta@tla.gov"
(juan 'attunements) ; → '("hushed whispers" "the unconquered wilderness" "careful duplicity")

(define juan-with-new-pw (juan 'pw "pendej0-supremo"))
(juan-with-new-pw 'name) ; → "juán peralta"
(juan-with-new-pw 'pw) ; → "pendej0-supremo"

(define clarabetta ((juan 'name "clarabetta kincaid") 'email "c.kincaid@tla.gov))

(clarabetta 'email) ; → "c.kincaid@tla.gov"

&nbsp;</code></pre>

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.
Z c9709df571935d4bc2f19a4da6564999