alias a name for the clause of a macro
Asked Answered
P

1

5

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?

Piles answered 13/3, 2016 at 22:12 Comment(0)
E
7

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))])
Elea answered 14/3, 2016 at 0:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.