Created
June 6, 2012 08:12
-
-
Save torus/2880611 to your computer and use it in GitHub Desktop.
util.match の練習。
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
| (use util.match) | |
| (define tree | |
| '((((1 2 3) (4 5) 6) ((7 8) 9 (10 11 12))) 13)) | |
| (define (match-tree t) | |
| (match t | |
| ((? number? n) | |
| (print "number " n)) | |
| ((children ...) | |
| (print "children") | |
| (for-each | |
| (lambda (child) | |
| (match-tree child)) | |
| children)) | |
| )) | |
| ;(match-tree tree) | |
| (define tree-2 | |
| '((1 "two") | |
| ("three" 4))) | |
| (define (match-tree-2 t) | |
| (match t | |
| ((((? number? n1) (? string? str1)) | |
| ((? number? n2) (? string? str2))) | |
| (print "number " n1 n2) | |
| (print "string " str1 str2)) | |
| ((((? number? n1) (? string? str1)) | |
| ((? string? str2) (? number? n2))) | |
| (print "number " n1 n2) | |
| (print "string " str1 str2)) | |
| ((((? string? str1) (? number? n1)) | |
| ((? number? n2) (? string? str2))) | |
| (print "number " n1 n2) | |
| (print "string " str1 str2)) | |
| ((((? string? str1) (? number? n1)) | |
| ((? string? str2) (? number? n2))) | |
| (print "number " n1 n2) | |
| (print "string " str1 str2)) | |
| )) | |
| ;(match-tree-2 tree-2) | |
| (define (match-tree-3 t) | |
| (define (match-tip p) | |
| (match p | |
| ((? number? n) | |
| (print "number " n)) | |
| ((? string? str) | |
| (print "number " str)))) | |
| (define (match-part p) | |
| (match p | |
| ((tips ...) | |
| (for-each match-tip tips)))) | |
| (match t | |
| ((part ...) | |
| (for-each match-part part)))) | |
| (match-tree-3 tree-2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment