s c h e m a t i c s : c o o k b o o k

/ WebHome / TOC / Cookbook.PatternMatchingChapter

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Introduction

Pattern matching is a particular convenient way to specify a case analysis on compound data and at the same time to deconstruct the data.

The essence of pattern matching is to compare the input data with set of patterns and when the first matching pattern is found, to perform an associated action. A typical match expression has the following form:

(match <expression>
  (<pattern_1> <expression_1>)
  ...)

The result of <expression> is compared to the patterns <pattern1> ... in turn, until the first matching pattern is found, then the associated <expression_n> is evaluated. If the pattern <pattern_n> contains pattern variables used to match arbitrary input, then these variables in <expression_n> are bound to the pieces of matched. Example: If the datum (1 2) is matched by the pattern (a b), then an associated expression (+ a b) will evaluate to 3.

There are several pattern matching libraries for Scheme. PLT Scheme comes with two, namely (lib "match.ss" "mzlib") and (lib "plt-match.ss" "mzlib"). The most important difference between them is pattern syntax, where plt-match is open to future enchancements.

You can view pattern matching as being similar to regular expressions, but acting on S-expressions instead of text strings. As in Regular Expressions, patterns allow you to bind parts of the data to variables that can be used in the expression part. Unlike regular expression systems, patterns can be used to do things like

  1. Match against the fields of a struct.
  2. Match based on the result of evaluating an expression.
  3. Use quasiquoting to make pattern templates.

Pattern matching is useful in handling markup (HTML or XML translated to S-Expressions), dispatching functions, and analyzing Scheme code, among other tasks.

Recipes



(edit comments)

Other matchers

IU-Match

The pattern matcher original written by Dan Friedman, then rewritten by Erik Hilsdale and later modified by Kent Dybvig.

This pattern matcher is used in (among others) Chez Scheme and SISC. match.ss

A PLT version can by found here

Mayer Goldbers pattern matcher

Goldbergs matcher

A naive pattern matcher implemented with syntax-rules

jas-match.scm

-- JensAxelSoegaard - 15 Aug 2004

CookbookForm
TopicType: Chapter
ParentTopic: TOC
TopicOrder: 11

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest