You can put your function in a module and require it both normally and for syntax.
#lang racket
(module my-function racket
(provide my-function)
(define (my-function x) (+ x 1)))
(require 'my-function (for-syntax 'my-function))
(define-syntax my-macro
(lambda (stx)
(datum->syntax stx (my-function (cadr (syntax->datum stx))))))
(my-macro 5)
(my-function 7)
See also cross phase persistent if you want one instantiation only.
Jos
--
You received this message because you are subscribed to the Google Groups "Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to racket-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/racket-users/244a3fc8-e24f-4121-a09f-3bd6d2c6b140n%40googlegroups.com.
Another solution:
#lang racket
(define-syntax (def-both-phases stx)
(syntax-case stx ()
((_ rest ...)
#'(begin
(define rest ...)
(define-for-syntax rest ...)))))
(def-both-phases (my-function x) (+ x 1))
(define-syntax my-macro
(lambda (stx)
(datum->syntax stx (my-function (cadr (syntax->datum stx))))))
(my-macro 5)
(my-function 7)
Jos
From: Yushuo Xiao
Sent: domingo, 9 de mayo de 2021 10:00
To: Racket Users
Subject: [racket-users] How to define a function that can be used both in syntax transformers and ordinary code?
I am using syntax transformers to define macros in Racket. I want to create some helper functions to help me manipulate the syntax. However, the functions I defined outside the syntax transformer are not available inside the syntax transformer. For example, in the following code
--