Last active
May 18, 2020 15:41
-
-
Save edw/1c663281adb06769b1d7b68971253f00 to your computer and use it in GitHub Desktop.
A working but not recommended macro
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
;;; This is not a good use of macros. See below for an | |
;;; example implementation of COND. | |
(define-syntax proc-all | |
(syntax-rules () | |
((_ exp () body ...) | |
(begin body ...)) | |
((_ exp (match matches ...) body ...) | |
(unless (= exp match) (proc-all exp (matches ...) body ...))))) | |
;;; Example | |
(proc-all 4 (3 2 1) (display "4 is not 3, 2, or 1.") (newline)) | |
;; Prints the message. | |
(proc-all 2 (3 2 1) (display "4 is not 3, 2, or 1.") (newline)) | |
;; Does not print the message. | |
;;; Something less crazy (COND including ELSE and => features) | |
(define-syntax my-cond | |
(syntax-rules (else =>) | |
((_) (begin)) | |
((_ (else body ...)) (begin body ...)) | |
((_ (test => proc) clauses ...) | |
(let ((result test)) | |
(if result | |
(proc result) | |
(my-cond clauses ...)))) | |
((_ (test body ...) clauses ...) | |
(if test (begin body ...) | |
(my-cond clauses ...))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment