Macro Name Alias

I would like to use several racket 2htdp functions / macros so that I can translate them into another language for my children.

Things that are functions, I can simply define . I have problems with the big-bang structure; If I try to make an on-tick alias, for example, every time I get big-bang: [new-name] clauses are not allowed within big-bang .

I tried various options for define-syntax , but I could not get it to work so far (this suggests that I am a complete newbie-newbie).

Something like this works (well, ladja 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 does not cause (causes an 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 this explains the problem: I assume that I would need to get my macro to be ranked first, somehow?

+5
source share
1 answer

If you are writing a module that you need in your program, you can use provide with rename-out to provide an alias:

In the big-bang-with-new-name.rkt file:

 #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 such keywords. Rename transformers collaborate with free-identifier=? to create exact aliases. This means that you can also define new-name as a rename transformer in the main file as follows:

 #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))]) 
+7
source

Source: https://habr.com/ru/post/1244976/


All Articles