The problem is that hello
is defined in the lexical scope of tinylang.rkt
but you want it to be in scope in try.rkt
. You can use datum->syntax
to set the lexical context of a piece of syntax.
This will fix the problem:
#lang racket
; here we redefine module begin.
(provide (all-defined-out)
(except-out (all-from-out racket) #%module-begin)
(rename-out [module-begin #%module-begin])
)
(define-syntax (module-begin stx)
(syntax-case stx ()
[(_ stmts ...)
#`(#%module-begin
#,(datum->syntax
stx
(syntax->datum
#'(define (hello) (print "Yes!") (newline))))
(print "Function defined.")
stmts ... )]))
UPDATE:
In response to comments, the previous solution could be simplified to:
(define-syntax (module-begin stx)
(syntax-case stx ()
[(_ stmts ...)
(with-syntax ([hello-fn (datum->syntax stx 'hello)])
#`(#%module-begin
(define (hello-fn) (print "Yes!") (newline))
(print "Function defined.")
stmts ... ))]))