I would like to alias some racket
2htdp
functions/macros, so that I can translate them in another language for my children.
Things which are functions I can simply alias with define
. I'm having trouble with the big-bang
structure; If I try to alias on-tick
for instance, everytime I get big-bang: [new-name] clauses are not allowed within big-bang
.
I tried various variants of define-syntax
but I could not make it work so far (that said, I'm a complete racket newbie).
Something like this works (well, ladja
is not defined):
#lang racket
(require 2htdp/universe 2htdp/image)
(big-bang 0
(on-tick (lambda (x) (+ x 1)))
(to-draw (lambda (x) (place-image ladja 150 x (prazni-prostor 300 300))))
(stop-when (lambda (x) (= x 300))))
But this doesn't (triggers the error):
#lang racket
(require 2htdp/universe 2htdp/image)
(define new-name on-tick)
(big-bang 0
(new-name (lambda (x) (+ x 1)))
(to-draw (lambda (x) (place-image ladja 150 x (prazni-prostor 300 300))))
(stop-when (lambda (x) (= x 300))))
I see that big-bang
is a macro, so that explains the issue: I guess I would have to be able to force my macro to be evaluated first, somehow?
If you're writing a module that you would require into your program, then you can use provide
with rename-out
to provide an alias:
In big-bang-with-new-name.rkt:
#lang racket
(require 2htdp/universe 2htdp/image)
(provide big-bang
to-draw
stop-when
empty-scene
(rename-out [on-tick new-name]))
Using it in another file:
#lang racket
(require "big-bang-with-new-name.rkt")
(big-bang 0
[new-name (lambda (x) (+ x 1))]
[to-draw (lambda (x) (empty-scene 200 200))]
[stop-when (lambda (x) (= x 300))])
Many macros use free-identifier=?
to recognize keywords like this. Rename transformers cooperate with free-identifier=?
to create exact aliases. This means you can also define new-name
as a rename transformer in the main file like this:
#lang racket
(require 2htdp/universe 2htdp/image)
(define-syntax new-name (make-rename-transformer #'on-tick))
(big-bang 0
[new-name (lambda (x) (+ x 1))]
[to-draw (lambda (x) (empty-scene 200 200))]
[stop-when (lambda (x) (= x 300))])
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