scheme - Naming variables using variables in Racket? -


if have 2 variables, example

(define x 10) (define y 20) 

and want create new variable, using values of x , y create name, how go doing so?

for example let's want make new variable called variable-x-y

(define variable-x-y "some-value") 

in case, x 10 , y 20.

basically summarize everything, want able enter variable-10-20 , have return "some-value"

i'm sorry if sounds novice question. i'm quite new racket.

edit: also, how go retrieving values if given values of x , y (within program)?

for example, let's somehow able define following:

(define variable-10-20 "some-value")  (define x 10) (define y 20) 

is there way me write variable-x-y , "some-value"?

edit 2 here simplified code of i'm trying implement. recursively reads each individual element local variable can used after it's been "read in". i'm sure if tweak code using method found here should work fine.

(define (matrix->variables matrix)   (local [(define (matrix2->variables/acc matrix2 x y)             (cond               [;; entire matrix has "extracted" it's elements variables                (empty? matrix2)                #|this main program goes using variables|#]               [;; first row has "extracted" it's elements variables                (empty? (first matrix2))                (matrix2->variables/acc (rest matrix2) 0 (add1 y))]               [else (local [(define element-x-y "some-value")]                       ;; here got stuck since couldn't find way                       ;; name variable being created (element-x-y)                       (matrix2->variables/acc                        (cons (rest (first matrix2)) (rest matrix2))                        (add1 x) y))]))]     (matrix2->variables/acc matrix 0 0))) 

i think you're misunderstanding how variable definition works. when create variable name, have know how call it, can't define names dynamically.

perhaps hash table storing bindings useful, it's similar ask , simulates having dynamically defined variables - still i'm not sure why want this, sounds more xy problem me. try this:

(define (create-key var1 var2)   (string->symbol    (string-append      "variable-"     (number->string var1)     "-"     (number->string var2))))  ; create new "variable" (define x 10) (define y 20) (create-key x y) => 'variable-10-20  ; use hash storing "variables" (define vars (make-hash))  ; add new "variable" hash (hash-set! vars (create-key x y) "some-value")  ; retrieve "variable" value hash (hash-ref vars 'variable-10-20) => "some-value" 

Comments

Popular posts from this blog

python - Specify path of savefig with pylab or matplotlib -

c# - SharpSsh Command Execution -

How to run C# code using mono without Xamarin in Android? -