mark.warren
2019-8-20 07:11:52

Racket news certificate seems to be out of date. Chrome is complaining.


spdegabrielle
2019-8-20 07:48:14

I’ve emailed Paulo.


mark.warren
2019-8-20 08:09:40

@spdegabrielle Thanks.


bvdbogae
2019-8-20 09:19:05

Mirroring using https is perfect. As for the email address, it would be indeed better to send messages to as the research one is not actively in use anymore. Thanks.


ruyvalle
2019-8-20 19:49:07

Hello! I am writing a macro that receives names of variables that are possibly defined at runtime. I want the macro to display their values. In some cases, the macro looks up variables with getenv and in some cases I want it to evaluate the variables by name. I am having trouble with the latter. I’ve been trying to use namespace-variable-value, but I don’t fully understand what it does.

Why does (let ([a 5]) (displayln (namespace-variable-value 'a))) give me a namespace-variable-value: given name is not defined error, whereas (begin (define a 5) (namespace-variable-value 'a)) works fine?

This is what my macro currently looks like: (define-syntax (get-rash-syntax-repr stx) (syntax-case stx (syntax quote) [(_ (syntax (quote thing))) #'thing] [(_ varname) #`(let* ([str-varname (symbol->string (syntax->datum varname))] [lookup (getenv str-varname)]) (cond [(string-prefix? str-varname "$") (namespace-variable-value (string->symbol (substring str-varname 1)))] [lookup lookup] [else str-varname]))]))

Thank you!


soegaard2
2019-8-20 19:50:15

The a in the let-expression is a local variable.


soegaard2
2019-8-20 19:51:13

The definition on the other hand is at the module top-level.


ruyvalle
2019-8-20 19:51:29

I see


soegaard2
2019-8-20 19:52:46

In Racket you can’t get an environment that contains the locally bound variables.


soegaard2
2019-8-20 19:53:07

It allows the compiler to generate faster code.


ruyvalle
2019-8-20 19:54:37

thank you


ruyvalle
2019-8-20 19:54:47

I’m now wondering how to do what I’m trying to do


soegaard2
2019-8-20 19:55:39

An example is (let ([a 1]) (+ a 2)).


soegaard2
2019-8-20 19:56:23

A compiler might rewrite this to (+ 1 2) and to 3. Thus not even allocating a variable at runtime.


ruyvalle
2019-8-20 19:56:42

right


soegaard2
2019-8-20 20:01:29

Is the variable name known at compile time?


ruyvalle
2019-8-20 20:02:01

its name/symbol is, but its value is not


soegaard2
2019-8-20 20:03:05

If the name is you can expand to #’varname since that will give you a variable reference.


ruyvalle
2019-8-20 20:06:58

That’s what I tried to do originally. The issue is the variable name will have a $ prepended to it. I think I tried the equivalent of #`#,(substring (symbol->string (syntax-datum #'varname)) 1) but that didn’t work either.


soegaard2
2019-8-20 20:11:29

I think we can fix that.


soegaard2
2019-8-20 20:13:02
#lang racket

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

(begin-for-syntax
  (define (remove-dollar id)
    (define $name (symbol->string (syntax->datum id)))
    (define  name (substring $name 1 (string-length $name)))
    (format-id id name)))


(define-syntax (lookup stx)
  (syntax-parse stx
    [(_lookup $name)
     (with-syntax ([name (remove-dollar #'$name)])
       #'name)]))

(let ([a 42])
  (lookup $a))

soegaard2
2019-8-20 20:14:00

Here (format-id id name) produces an identifier name with the same lexical context as id.


ruyvalle
2019-8-20 20:14:44

cool!


ruyvalle
2019-8-20 20:14:59

I think this gives me what I need. Thank you!