source: project/wiki/eggref/4/datatype @ 13306

Last change on this file since 13306 was 13306, checked in by felix winkelmann, 12 years ago

added xlib page and removed download links

File size: 3.4 KB
Line 
1[[tags: egg]]
2
3== datatype
4
5[[toc:]]
6
7=== Description
8
9A facility for creating and using variant records, as described in the
10book "Essentials of Programming Languages" by Friedman, Wand and
11Haynes.
12
13=== Author
14
15[[felix winkelmann]]
16
17=== Requirements
18
19None
20
21=== Documentation
22
23<macro>(define-datatype TYPENAME [PREDICATE] VARIANT ...)</macro>
24
25Defines a record type named {{TYPENAME}}, where {{VARIANT ...}}
26defines one or more constructors for instances of this type. {{VARIANT}} should
27be of the form
28
29  VARIANT = (CONSTRUCTOR (FIELDNAME FIELDPRED) ...)
30
31{{CONSTRUCTOR}} is the name of a constructor procedure that will be defined
32with as many arguments as fields are specified. {{(FIELDNAME FIELDPRED) ...}}
33specify the name for each field and a procedure of one argment that should return a true
34value for legal field values.
35
36The optional {{PREDICATE}} should be the name of a procedure that will
37be defined and which returns {{#t}} when applied to an instance of
38this variant record.
39
40<macro>(cases TYPENAME EXP CLAUSE ...)</macro>
41
42A facility for matching and deconstructing the instance {{EXP}} of the
43variant record with the name {{TYPENAME}}. Each {{CLAUSE}} specifies a
44constructor with field-names and a body to execute when the
45constructor matches the record instance:
46
47  CLAUSE = (CONSTRUCTOR (FIELDNAME ...) BODY ...)
48         | (else BODY ...)
49
50
51==== Example
52
53<examples><example>
54<expr>
55; A simple binary tree with numbers:
56
57(require-for-syntax 'datatype)
58
59(define-datatype tree tree?
60  (leaf (n number?))
61  (branch (left tree?) (right tree?)) )
62
63(define t (branch (branch (leaf 33) (leaf 44)) (leaf 55)))
64
65(define (listify t)
66  (cases tree t
67    (leaf (n) n)
68    (branch (left right) (cons (listify left) (listify right))) ) )
69
70(listify t)
71</expr>
72<result>((33 . 44) . 55)</result>
73</example></examples>
74
75=== Changelog
76
77* 1.2 Added missing {{datatype.html}} [Thanks to Ivan Raikov]
78* 1.1 Adapted to new setup scheme
79* 1.0
80
81=== License
82
83  Copyright (c) 2003, Felix L. Winkelmann
84  All rights reserved.
85 
86  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
87  conditions are met:
88 
89    Redistributions of source code must retain the above copyright notice, this list of conditions and the following
90      disclaimer.
91    Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
92      disclaimer in the documentation and/or other materials provided with the distribution.
93    Neither the name of the author nor the names of its contributors may be used to endorse or promote
94      products derived from this software without specific prior written permission.
95 
96  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
97  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
98  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
99  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
100  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
101  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
102  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
103  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
104  POSSIBILITY OF SUCH DAMAGE.
Note: See TracBrowser for help on using the repository browser.