[racket] Compile-time evaluation

From: Vincent St-Amour (stamourv at ccs.neu.edu)
Date: Mon Jul 29 13:53:32 EDT 2013

Here's a quick solution:

    #lang racket

    (define-syntax-rule (hash-dot e)
      (let-syntax ([computed-e (lambda (stx) (datum->syntax #'e e))])
        (computed-e)))

    (hash-dot (+ 2 3))


This defines a local macro that computes the value of `e' at compile
time. Any binding that you use in `e' will need to be bound at syntax
time (which, in #lang racket, is all of racket/base), which may mean
using `(require (for-syntax X))'.

If you try to compute, say, a function at compile-time, you'll run into
trouble, though.

Vincent



At Mon, 29 Jul 2013 06:25:21 +0400,
Roman Klochkov wrote:
> 
> [1  <multipart/alternative (7bit)>]
> [1.1  <text/plain; utf-8 (base64)>]
>  Is there simple way to calculate during compilation. Something like #. in Common Lisp.
> 
> For example how to do something like
> (case ftype
>   ((#.(keyword->g-type 'enum)
>     #.(keyword->g-type 'flags))
>         (convert-to-foreign value (g-type->lisp type)))
>   (#.(keyword->g-type 'double)
>         (coerce value 'double-float))
>   (#.(keyword->g-type 'float)
>         (coerce value 'single-float))
>   ((#.(keyword->g-type 'int)
>     #.(keyword->g-type 'uint)
>     #.(keyword->g-type 'long)
>     #.(keyword->g-type 'ulong)
>     #.(keyword->g-type 'int64)
>     #.(keyword->g-type 'uint64)) 
>         (round value))
>   (else value))
> 
> 
> 
> where keyword->g-type is a function (-> symbol? exact-integer?).
> 
> I don't want to place resulting numbers, because then there will be a lot of "magic numbers" corresponfing to different foreign types. 
> 
> 
> -- 
> Roman Klochkov
> [1.2  <text/html; utf-8 (base64)>]
> 
> [2  <text/plain; us-ascii (7bit)>]
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users


Posted on the users mailing list.