Skip to content

Instantly share code, notes, and snippets.

@jclosure
Created July 31, 2014 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jclosure/b4d6021a30cf120942d7 to your computer and use it in GitHub Desktop.
Save jclosure/b4d6021a30cf120942d7 to your computer and use it in GitHub Desktop.
The case for scheme macros
;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