Often times it is useful to preload a table of data at compile-time. Scheme, however, has no construct similar to Lisp's eval-when which allows specific compile-time programming.
Although Scheme has no specific compile-time stage, most Scheme systems can and do perform macro-expansion during compilation. Therefore, the following macro can easily be used to evaluate Scheme code during compile time so that the results are available at runtime.
;;Define our macro
(define-syntaxat-expand-time;;x is the syntax object to be transformed
(lambda (x)
(syntax-casex ()
(
;;Pattern just like a syntax-rules pattern
(at-compile-timeexpression)
;;with-syntax allows us to build syntax objects
;;dynamically
(with-syntax
(
;this is the syntax object we are building
(expression-value;after computing expression, transform it into a syntax
object
(datum->syntax-object;syntax domain
(syntaxat-compile-time)
;quote the value so that its a literal value
(list'quote;compute the value to transform
(eval;;convert the expression from the syntax
;;representation to a list representation
(syntax-object->datum (syntaxexpression)))))))
;;Just return the generated value as the result
(syntaxexpression-value))))))
Here is some code that generates, at compile time, a list of square roots from 0 to 25:
Although there are not guarantees about when macro expansion will occur, this little tool is better than nothing. It has been tested on Chicken Scheme.