Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
Last active December 16, 2015 18:59
Show Gist options
  • Save tokiwoousaka/5481512 to your computer and use it in GitHub Desktop.
Save tokiwoousaka/5481512 to your computer and use it in GitHub Desktop.
egisonを使ったMaybeのmatcher定義
(define $maybe
(lambda [$a]
(matcher {
;; <primitive-pp>
[,$val [a] {[$tgt
(match [val tgt] [(maybe a) (maybe a)] {
[[<just $x> <just ,x>] {[x]}]
[[<nothing> <nothing>] {[]}]
[_ {}]})]}]
;; <primitive-dp>
[<just $> [a] {[<Just $x> {[x]}] [_ {}]}]
[<nothing> [] {[<Nothing> {[]}] [_ {}]}]})))
;; <OK> patterns
(test "------------")
(test (match <Just 5> (maybe integer) {[,<Just 5> <OK>] [_ <KO>]}))
(test (match <Nothing> (maybe integer) {[,<Nothing> <OK>] [_ <KO>]}))
(test (match <Just 10> (maybe integer) {[<just ,10> <OK>] [_ <KO>]}))
(test (match <Nothing> (maybe integer) {[<nothing> <OK>] [_ <KO>]}))
(test (match <Just {1 2 3}> (maybe (list integer)) {[<just <cons ,1 _>> <OK>] [_ <KO>]}))
(test (match <Just {2 1 3}> (maybe (multiset integer)) {[<just <cons ,1 _>> <OK>] [_ <KO>]}))
;; <KO> patterns
(test "------------")
(test (match <Just 5> (maybe integer) {[,<Nothing> <OK>] [_ <KO>]}))
(test (match <Nothing> (maybe integer) {[,<Just 10> <OK>] [_ <KO>]}))
(test (match <Just 10> (maybe integer) {[<just ,8> <OK>] [_ <KO>]}))
(test (match <Just 15> (maybe integer) {[<nothing> <OK>] [_ <KO>]}))
(test (match <Nothing> (maybe integer) {[<just _> <OK>] [_ <KO>]}))
(test (match <Just {1 2 3}> (maybe (list integer)) {[<just <cons ,3 _>> <OK>] [_ <KO>]}))
(test (match <Just {2 1 3}> (maybe (multiset integer)) {[<just <nil>> <OK>] [_ <KO>]}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment