[racket] Compile-time evaluation

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Mon Jul 29 20:28:49 EDT 2013

 Thank you. This works just fine in any place, where function is expected.
But the case with "case" still doesn't work

(case 5
    [((hash-dot (+ 2 3))) 1]
    [else 2])
=> 2

I think, that I need to do something with the Racket reader.


Понедельник, 29 июля 2013, 13:53 -04:00 от Vincent St-Amour <stamourv at ccs.neu.edu>:
>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


-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130730/88b15d89/attachment.html>

Posted on the users mailing list.