Skip to content

Instantly share code, notes, and snippets.

@vbuaraujo
Last active April 15, 2017 01:16
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 vbuaraujo/c99c31a714174aa82d7a8ffdd043d97f to your computer and use it in GitHub Desktop.
Save vbuaraujo/c99c31a714174aa82d7a8ffdd043d97f to your computer and use it in GitHub Desktop.
Extended 'if' syntax, allowing 'else' and 'elif' clauses
(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