📄 pattern-matching.html
字号:
<html><head><title>CHICKEN User's Manual - Pattern matching</title></head><body><p> </p><a name="pattern-matching"></a><h1>Pattern matching</h1><p>(This description has been taken mostly from Andrew Wright's postscript document)</p><p>Pattern matching allows complicated control decisions based on data structure to be expressed in a concise manner. Pattern matching is found in several modern languages, notably Standard ML, Haskell and Miranda. These syntactic extensions internally use the <tt>match</tt> library unit.</p><p>Note: this pattern matching package is not compatible with hygienic macro-expanders like the <tt>syntax-case</tt> extension (available separately).</p><p>The basic form of pattern matching expression is:</p><PRE>(match exp [pat body] ...)</PRE><p>where <tt>exp</tt> is an expression, <tt>pat</tt> is a pattern, and <tt>body</tt> is one or more expressions (like the body of a lambda-expression). The <tt>match</tt> form matches its first subexpression against a sequence of patterns, and branches to the <tt>body</tt> corresponding to the first pattern successfully matched. For example, the following code defines the usual <tt>map</tt> function:</p><PRE>(<B><FONT COLOR="#A020F0">define</FONT></B> <B><FONT COLOR="#0000FF">map</FONT></B> (<B><FONT COLOR="#A020F0">lambda</FONT></B> (f l) (match l [() '()] [(x . y) (cons (f x) (map f y))])))</PRE><p>The first pattern <tt>()</tt> matches the empty list. The second pattern <tt>(x . y)</tt> matches a pair, binding <tt>x</tt> to the first component of the pair and <tt>y</tt> to the second component of the pair.</p><a name="pattern-matching-expressions"></a><h2>Pattern Matching Expressions</h2><p>The complete syntax of the pattern matching expressions follows:</p><pre>exp ::= (match exp clause ...) | (match-lambda clause ...) | (match-lambda* clause ...) | (match-let ([pat exp] ...) body) | (match-let* ([pat exp] ...) body) | (match-letrec ([pat exp] ...) body) | (match-let var ([pat exp] ...) body) | (match-define pat exp)</pre><pre>clause ::= [pat body] | [pat (=> identifier) body]</pre><pre>pat ::= identifier matches anything, and binds identifier as a variable | _ anything | () itself (the empty list) | #t itself | #f itself | string an `equal?' string | number an `equal?' number | character an `equal?' character | 's-expression an `equal?' s-expression | (pat-1 ... pat-n) a proper list of n elements | (pat-1 ... pat-n . pat-n+1) a list of n or more elements | (pat-1 ... pat-n pat-n+1 ..k) a proper list of n+k or more elements [1] | #(pat-1 ... pat-n) a vector of n elements | #(pat-1 ... pat-n pat-n+1 ..k) a vector of n+k or more elements | ($ struct pat-1 ... pat-n) a structure | (= field pat) a field of a structure | (and pat-1 ... pat-n) if all of pat-1 through pat-n match | (or pat-1 ... pat-n) if any of pat-1 through pat-n match | (not pat-1 ... pat-n) if none of pat-1 through pat-n match | (? predicate pat-1 ... pat-n) if predicate true and pat-1 through pat-n all match | (set! identifier) anything, and binds identifier as a setter | (get! identifier) anything, and binds identifier as a getter | `qp a quasipattern</pre><pre>qp ::= () itself (the empty list) | #t itself
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -