Gauche でパターンマッチ

11.47 util.match - パターンマッチング

Haskell でパターンマッチ分岐すると他の言語でもパターンマッチしたくなる。

(use util.match)

(define (match-test ls)
  (match ls
    (((a b) c (d e)) (list "a:" a b c d e))
    (((a b))         (list "b:" a b))
    (((a b) c)       (list "c:" (list a b) c))
    (else           (list "else") )))

(print (match-test '((1 2) 3 (4 5))))  ;=> (a: 1 2 3 4 5)
(print (match-test '(("abc" 123))))    ;=> (b: abc 123)
(print (match-test '((#\a #\b) #\c)))  ;=> (c: (a b) c)
(print (match-test '(1 2 3)))          ;=> (else)