[r6rs-discuss] [scheme-reports] Scheme pattern matching & R*RS
per at bothner.com
Sat Jan 22 12:09:01 EST 2011
On 01/22/2011 08:23 AM, David Van Horn wrote:
> On 1/22/11 10:56 AM, Andre van Tonder wrote:
>> ML or Haskell programs are chock full of the former, which looks cool
>> exactly until they want to add an extra field to a data type and all of
>> a sudden realize that they have to change their whole program and all
>> programs that import the same types.
> That doesn't *have* to be the case. Here's an example Racket program
> that uses pattern matching on a data type extended with an extra field:
> #lang racket
> (define-struct node (left right))
> (define-struct (fancy node) (color))
> (match (fancy 'l 'r 'c)
> [(node l r) (list l r)])
That woould be less of a problem with named fields/properties.
(match (fancy 'l 'r 'c)
[(node left: l right: r) (list l r)])
This assumes that a syntax or convention for object
construction like this (which Kawa uses):
(TYPE NAME1: VALUE1 NAME2: VALUE2 ...)
Hence it is natural to support pattern-match using:
PATTERN ::= ....
| (TYPE NAME1: PATTERN1 NAME2: PATTERN2 ...)
The semantics would be TYPE has to be a supertype of the
matchee's type, the matchee has at least the named fields/properties
(or perhaps: TYPE has at least the named fields/properties),
the corresponding field values match the corresponding patterns.
per at bothner.com http://per.bothner.com/
More information about the r6rs-discuss