Last active
December 22, 2015 17:09
-
-
Save leque/6504203 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; pattern guards a la Haskell | |
;;; 元ネタ: http://d.hatena.ne.jp/gengar/20130909/1378719785 | |
(use util.match) | |
(define-syntax do-match* | |
(syntax-rules () | |
((_ binds body ...) | |
(%do-match* #f binds body ...)))) | |
(define-syntax %do-match* | |
(syntax-rules () | |
((_ cont () body ...) | |
(let () body ...)) | |
((_ #f ((pat expr) rest ...) body ...) | |
(match expr | |
(pat | |
(=> cont) | |
(%do-match* cont (rest ...) body ...)) | |
(_ #f))) | |
((_ cont ((pat expr) rest ...) body ...) | |
(match expr | |
(pat | |
(%do-match* cont (rest ...) body ...)) | |
(_ (cont)))))) | |
(define (add-assoc alist x y) | |
(or (do-match* (((_ . m) (assoc x alist)) | |
((_ . n) (assoc y alist))) | |
(+ m n)) | |
0)) | |
(print (add-assoc '((1 . 2) | |
(3 . 4)) | |
1 3)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment