Skip to content

Instantly share code, notes, and snippets.

@takei-shg
Forked from tokiwoousaka/gist:5481512
Created May 1, 2013 06:18
Show Gist options
  • Save takei-shg/5493999 to your computer and use it in GitHub Desktop.
Save takei-shg/5493999 to your computer and use it in GitHub Desktop.
(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