[racket] Compile-time evaluation

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Mon Jul 29 13:32:13 EDT 2013

On 2013-07-29 06:25:21 +0400, Roman Klochkov wrote:
>    Is there simple way to calculate during compilation. Something like #.
>    in Common Lisp.

Can you tell us what #. means in Common Lisp?

I am guessing that you want to do something like the following:

  #lang racket
  (require (for-syntax syntax/parse))

  ;; functions to compute costly constants
  ;; at compile-time
  (begin-for-syntax
    (define (compute-x) ...)
    (define (compute-y) ...)
    (define (compute-z) ...))

  ;; macro that defines x, y, z assuming
  ;; suitable definitions of compute-x, etc.
  (define-syntax (define-constants stx)
    (syntax-parse stx
      [(_ x:id y:id z:id)
       #`(begin (define x #,(compute-x))
                (define y #,(compute-y))
                (define z #,(compute-z)))]))

  (define-constants my-x my-y my-z)

This will work if the `compute-` functions produce flat data that can be
embedded in syntax, like numbers.

Cheers,
Asumu

Posted on the users mailing list.