Created
July 31, 2014 02:57
-
-
Save jclosure/b4d6021a30cf120942d7 to your computer and use it in GitHub Desktop.
The case for scheme macros
This file contains 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
;3-state condition defined as a function | |
(define (3-state | |
value | |
positive-body | |
zero-body | |
negative-body) | |
(cond | |
((zero? value) zero-body) | |
((positive? value) positive-body) | |
(else negative-body))) | |
;doesn't work because of eager eval | |
(3-state 100 (display "P") (display "Z") (display "N")) | |
; => NZP | |
;3-state condition defined as a macro | |
(define-syntax | |
3-state | |
(syntax-rules () | |
((3-state value positive-body zero-body negative-body) | |
(cond | |
((zero? value) zero-body) | |
((positive? value) positive-body) | |
(else negative-body))))) | |
;does work correctly when pattern is matched and dispatched | |
(3-state 100 (display "P") (display "Z") (display "N")) | |
; => P |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment