Last active
April 15, 2017 01:16
-
-
Save vbuaraujo/c99c31a714174aa82d7a8ffdd043d97f to your computer and use it in GitHub Desktop.
Extended 'if' syntax, allowing 'else' and 'elif' clauses
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
(define-module (elmord extended-if) | |
#:export (if*)) | |
(define-syntax if* | |
(syntax-rules (else elif) | |
;; Subsume the standard 'if'. | |
[(if* condition form1) (if condition form1)] | |
[(if* condition form1 form2) (if condition form1 form2)] | |
;; If more forms present, use extra syntax. | |
[(if* condition forms ...) | |
(if/sub condition () forms ...)])) | |
(define-syntax if/sub | |
(syntax-rules (else elif) | |
[(if/sub condition (then-forms ...) else else-forms ...) | |
(if condition (begin then-forms ...) (begin else-forms ...))] | |
[(if/sub condition1 (then-forms ...) elif condition2 rest ...) | |
(if condition1 | |
(begin then-forms ...) | |
(if/sub condition2 () rest ...))] | |
[(if/sub condition (then-forms ...) then-form rest ...) | |
(if/sub condition (then-forms ... then-form) rest ...)])) | |
;;; Example. | |
(define (signum x) | |
(if* (> x 0) | |
(display "positive\n") | |
+1 | |
elif (< x 0) | |
(display "negative\n") | |
-1 | |
else | |
(display "zero\n") | |
0)) | |
(signum 42) | |
(signum 0) | |
(signum -42) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment