How does one create aliases for macros in Scheme?
in the same vein as this for Clojure
define a synonym for a Clojure macro
and this for Common Lisp
common lisp function/macro aliases
I realize for functions it is
(define alias function)
but I can't seem to find, or come up with, how to define aliases for macros (in GNU Guile to be precise)
If you are using a scheme implementation which supports R6RS, such as Chez Scheme or guile, the simplest approach is to use identifier-syntax to create an alias:
(define-syntax new-name (identifier-syntax old-name))
identifier-syntax can do more than alias a name: it can alias an expression. So you could define an **unspecified** macro as follows:
(define-syntax **unspecified** (identifier-syntax (if #f #f)))
Guile supports syntax-rules, so a plausible way to create an alias for, say, `and' would look like this:
;; define my-and as an alias for and:
(define-syntax my-and
(syntax-rules ()
[(_ arg ...)
(and arg ...)]))
;; try it out:
(display (my-and #t #t)) (newline)
(display (my-and #t #f)) (newline)
(display (my-and #f (/ 1 0))) (newline)
If you really wanted a syntax-alias form, you could... surprise! ... define that as a macro:
;; define a 'syntax-alias' form
(define-syntax syntax-alias
(syntax-rules ()
[(_ newname oldname)
(define-syntax newname
(syntax-rules ()
[(_ arg (... ...))
(oldname arg (... ...))]))]))
;; use 'syntax-alias' to define 'my-and' as an alias for 'and':
(syntax-alias my-and and)
;; try it out:
(display (my-and #t #t)) (newline)
(display (my-and #t #f)) (newline)
(display (my-and #f (/ 1 0))) (newline)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With