8.16.0.1
2.3 模式匹配
azelf包含整个racket/match,该模块是对racket/match的一个补充。
syntax
(define/match1 函数名 函数体 ...)
Examples:
> (define/match1 f [1 2] [_ 10]) > (f 1) 2
> (f 2) 10
> (define/match1 g [1 (+ it it it)] [_ 10]) > (g 1) 3
> (g 2) 10
syntax
(define/match1/contract 函数名 contract-body 函数体 ...)
像我这么在乎安全的人,怎能少了racket/contract。
Examples:
> (define/match1/contract f (-> positive? positive?) [1 2] [_ -1]) > (f 1) 2
> (f 10) f: broke its own contract
promised: positive?
produced: -1
in: the range of
(-> positive? positive?)
contract from: (function f)
blaming: (function f)
(assuming the contract is correct)
at: eval:10:0
syntax
(define/match/contract (函数名 参数 ...) contract-body 函数体 ....)
我们也给define/match加个racket/contract。
Examples:
> (define/match/contract (f a) (-> positive? positive?) [(1) 2] [(_) -10]) > (f 1) 2
> (f 2) f: broke its own contract
promised: positive?
produced: -10
in: the range of
(-> positive? positive?)
contract from: (function f)
blaming: (function f)
(assuming the contract is correct)
at: eval:13:0