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

Last change on this file since 12166 was 12166, checked in by Ivan Raikov, 12 years ago

Added pages for release 4 eggs.

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