Reputation: 24555
If n is defined as 5, (- n) will give a value of -5. Can there be a macro to identify "-n" as negative of n or (- n) in Racket language?
> (define n 5)
> n
5
> (- n)
-5
> (-n)
. . -n: undefined;
cannot reference an identifier before its definition
> -n
. . -n: undefined;
cannot reference an identifier before its definition
>
Upvotes: 0
Views: 920
Reputation: 31147
Yes. If we assume you have no variables in your program with an identifier that begins with a dash you can abuse #%top
.
The expression -n
is a variable reference to an unbound variable.
The expander will turn -n
into (#%top . -n)
.
If you in module negative.rkt
write a macro named my-top
and provide it using
(provide (rename-out [my-top #%top]))
then you can write (require "negative.rkt")
to use your own version of #%top
.
The definition of my-top
is something like:
if the input identifier x begins with -
then turn -something
into (- something)
otherwise return (#%top . x)
.
See this question for an example of how to redefine #%top
: Macro of [S:N] for in-range in Racket
Upvotes: 2