s c h e m a t i c s : c o o k b o o k

/ Cookbook.AtExpandTime

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

A Macro Similar to CL's Eval-When

Problem

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.

Solution

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)

Discussion

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.


Comments about this recipe

Contributors

-- JonathanBartlett - 31 May 2005

CookbookForm
TopicType: Recipe
ParentTopic: DynamicRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest