Instantly share code, notes, and snippets.

# パターンマッチ指向プログラミング

``````(define \$map
(lambda [\$f \$xs]
(match-all xs (list something)
[<join _ <cons \$x _>> (f x)])))
``````

キーワード: something, eq, list, multiset, cons, join, predicate pattern, not-pattern, later pattern

## 問題

``````(define \$member?
(lambda [\$x \$xs]
(match xs (list eq)
{[ _1_ _2_ ]
[_ #f]})))
``````

## 問題

ヒント: consとjoinの逆の意味をもつsnocとniojパターンコンストラクタを使う．

``````(define \$reverse
(lambda [\$xs]
(match-all xs (list something)
[ _1_ _2_ ])))
``````

## 問題

``````(define \$concat
(lambda [\$xss]
(match-all xss _1_
[ _2_ _3_ ])))
``````

## 問題

``````(define \$intersect
(lambda [\$xs \$ys]
(match-all [xs ys] _1_
[ _2_ _3_ ])))
``````

## 問題

``````(define \$unique
(lambda [\$xs]
(match-all xs (list eq)
[ _1_ _2_ ])))
``````

``````(unique {1 2 3 2 4})
;{1 3 2 4}
``````

``````(unique {1 2 3 2 4})
;{1 2 3 4}
``````

# マッチャー定義

## 問題

``````  (algebraic-data-matcher

(define \$card
(matcher
{[<card \$ \$> [suit (mod 13)]
{[<Card \$x \$y> {[x y]}]}]
[\$ [something] {[\$tgt {tgt}]}]}))
``````

たとえば，以下の場合について，"Straight flush"と返せるようにせよ．

``````(poker-hands {<Card <Club> 12>
<Card <Club> 10>
<Joker>
<Card <Club> 1>
<Card <Club> 11>})
``````