[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [r6rs-discuss] [scheme-reports] Scheme pattern matching & R*RS



On 1/22/11 10:56 AM, Andre van Tonder wrote:
> What I mean is that pattern matching often does not encourage good
> abstraction. Which of the following is the better abstracted program
> (independent of the underlying data type and independent of, for
> example, adding new fields in data types )?
>
> (match data
>   ((record node l r) (traverse l)
>                      (traverse r))
>   ((leaf x) (display x))
>
> or
>
> (cond ((node? data) (traverse (node-left data))
>                     (traverse (node-right data))
>       ((leaf? data) (display (leaf-content data))
>
> 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)])

David

_______________________________________________
r6rs-discuss mailing list
r6rs-discuss@x
http://lists.r6rs.org/cgi-bin/mailman/listinfo/r6rs-discuss