5

I am pretty new in Racket and I have tried to run Chris Jester-Young's solution (How to convert string to variable-name in scheme) to convert string to variable name (and its converse, in other words, conversing variable name to string):

#lang racket
(define-syntax string->variable-name
   (lambda (stx)
   (syntax-case stx ()
     ((_ str)
       (string? (syntax->datum #'str))
         (datum->syntax #'str (string->symbol (syntax->datum #'str)))))))

(define-syntax variable-name->string
  (lambda (stx)
    (syntax-case stx ()
    ((_ id)
       (identifier? #'id)
         (datum->syntax #'id (symbol->string (syntax->datum #'id)))))))

It was fine for variable-name->string, here an example:

>(define myvar 10)
>(variable-name->string myvar)
  "myvar"

However, I had no success with string->variable-name:

>(define (string->variable-name "myvar2") 4)
    error define: not an identifier, identifier with default, or keyword
    for procedure argument in: "myvar2"

I am using Racket 6.6, linux mint 17.3 64bits.

My main goal is how to convert a string to a variable name (identifier).

I was wondering:

  1. to read a text file;
  2. to get the name of the variables (string) from that text file; and
  3. to use define to set the variable names with random integers.

Any help I will appreciate.

4
  • 1
    Use (variable-name->string foo) to invoke a macro with the name variable-name->string. The (define (variable-name->string ... means that you are defining a function named variable-name->string and since "myvar2" is not a legal name for a function argument, you get an error. Commented Aug 17, 2016 at 11:14
  • What are you trying to do with this? The macro that changes from a variable to a string does so compile time. The other way needs to be aswell so you can never make anything useful with these later. Commented Aug 17, 2016 at 12:32
  • Reading plain text file is a requirement for an academic work. Moreover, I have to build a program which is able to: 1- read a plain text file; 2- extract some strings from that plain text file; 3- assign that strings to variable names, dynamically; 4- do some calculation with that variables; and 5- finally, write down the outcomes to a plain text file. I have got success in all those steps, in exception with step 3. Commented Aug 17, 2016 at 21:53
  • Hope you have learned what's happening. @soegaard 's response is correct; just call (define myvar2 1) before the other call Commented Aug 15, 2018 at 0:23

1 Answer 1

0

You may check answers from here.

I was advised not to use strings but identifiers. So instead of having a plain text file you may use another Racket source with identifiers you want to use and just import this one.

Sign up to request clarification or add additional context in comments.

2 Comments

Reading plain text file is a requirement for an academic work. Moreover, I have to build a program which is able to: 1- read a plain text file; 2- extract some strings from that plain text file; 3- assign that strings to variable names, dynamically; 4- do some calculation with that variables; and 5- finally, write down the outcomes to a plain text file. I have got success in all those steps, in exception with step 3.
Trying to give more details of what I am trying to do: (define temp_var "Xi") ;the string "Xi" was extracted from a text file. (define (display temp_var) 2 ) ;is it the same of (define Xi 2)? (display Xi) ;error Xi was not defined. (+ Xi 3) ; is the outcome equals to 5?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.