This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Pattern Matching


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

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]