Skip to content

Instantly share code, notes, and snippets.

@tokiwoousaka
Last active December 16, 2015 20:58
Show Gist options
  • Save tokiwoousaka/5495959 to your computer and use it in GitHub Desktop.
Save tokiwoousaka/5495959 to your computer and use it in GitHub Desktop.
たけとんさんの書いたeitherで遊んでみた
(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> {[]}] [_ {}]}]})))
(define $either
(lambda [$a $b]
(matcher {
;; <primitive-pp>
[,$val [a b] {[$tgt
(match [val tgt] [(either a b) (either a b)] {
[[<left $x> <left ,x>] {[x]}]
[[<right $y> <right ,y>] {[y]}]
[_ {}]
})
]}]
;; <primitive-dp>
[<left $> [a] {
[<Left $x> {[x]}]
[_ {}]
}]
[<right $> [b] {
[<Right $x> {[x]}]
[_ {}]
}]
})
)
)
(test "------------")
(define $my-func
(lambda $x
(match x (either (maybe string) (list integer)) {
[<left <just _>> <Pattern1>]
[<left <nothing>> <Pattern2>]
[<left <cons $x $y>> <Pattern3 x y>]
[<right <cons ,1 _>> <Pattern4>]
[<right <cons ,999 $x>> <Pattern5 x>]
;;[<right <nothing>> <Pattern6>] ;;Error
[<right <cons $x $y>> <Pattern7 x y>]})))
(test (my-func <Left <Just 5>>))
(test (my-func <Left <Nothing>>))
;;(test (my-func <Left {1 2 3}>)) ;;Error
(test (my-func <Right {1 2 3 4}>))
(test (my-func <Right {999 2 3 4}>))
;;(test (my-func <Right <Nothing>>))
(test (my-func <Right {2 4 6 8}>))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment