Skip to content

Instantly share code, notes, and snippets.

@vbuaraujo
Created February 23, 2018 19:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbuaraujo/5d177986e4b031e48e96ffbeb8fb4464 to your computer and use it in GitHub Desktop.
Save vbuaraujo/5d177986e4b031e48e96ffbeb8fb4464 to your computer and use it in GitHub Desktop.
(define-syntax switch
(syntax-rules (default)
[(switch expr clauses ...)
(let ([result expr])
(%handle-switch-clauses result clauses ...))]))
(define-syntax %handle-switch-clauses
(syntax-rules (default)
[(_ result) 'Nada™]
[(_ result (default forms ...)) (begin forms ...)]
[(_ result (value forms ...) other-clauses ...)
(if (equal? result value)
(begin forms ...)
(%handle-switch-clauses result other-clauses ...))]))
(define n 1)
(switch n
[1 (display "foo\n")]
[2 (display "bar\n")]
[default (display "other\n")])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment