This is the mail archive of the
guile@sourceware.cygnus.com
mailing list for the Guile project.
Pattern Matching
- To: guile at sourceware dot cygnus dot com
- Subject: Pattern Matching
- From: Diego Dainese <ddainese at dsi dot unive dot it>
- Date: 23 Apr 2000 14:27:39 +0200
Hi,
Does Guile support some form of pattern matching?
I mean something like the SML's one; this is my idea:
- a function to define a discriminated union; in example
(define-union <tree> ((%nil)
(%node info subtree1 subtree2)))
note: %nil and %node are type constructor, and can be used to
instantiate unions:
(define t (%node "test node" %nil %nil))
- a special form that decomposes an union into its components, matches
them against a pattern, and if the matching is successful bind them
to variable names and execute a body; in example, here is a function
that return the size of an instance of the tree defined above:
(define (tree-size t)
(pattern-match t
((%nil)
0)
((%node i t1 t2)
(+ 1 (tree-size t1) (tree-size t2)))))
Bye,
--
Diego Dainese