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-syntax at-expand-time
;;x is the syntax object to be transformed
(lambda (x)
(syntax-case x ()
(
;;Pattern just like a syntax-rules pattern
(at-compile-time expression)
;;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
(syntax at-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 (syntax expression)))))))
;;Just return the generated value as the result
(syntax expression-value))))))
Here is some code that generates, at compile time, a list of square roots from 0 to 25:
(define sqrt-table
(at-expand-time
(list->vector
(let build
(
(val 0))
(if (> val 25)
'()
(cons (sqrt val) (build (+ val 1))))))))
(display "the square root of 24 is ")
(display (vector-ref sqrt-table 24))
(newline)
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.