使用 Racket 中的变量命名变量? [英] Naming variables using variables in Racket?

查看:36
本文介绍了使用 Racket 中的变量命名变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有两个变量,例如

If I have two variables, for example

(define x 10)
(define y 20)

我想创建一个新变量,使用 xy 的值来创建名字,我该怎么做?

And I want to create a new variable, using the values of x and y to create the name, how would I go about doing so?

例如,假设我想创建一个名为 variable-x-y

For example let's say I want to make a new variable called variable-x-y

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

在这种情况下,x 为 10,y 为 20.

In this case, x would be 10 and y would be 20.

基本上总结一切,我希望能够输入 variable-10-20 并让它返回 "some-value"

Basically to summarize everything, I want to be able to enter variable-10-20 and have it return "some-value"

如果这听起来像一个新手问题,我很抱歉.我是 Racket 的新手.

I'm sorry if this sounds like a novice question. I'm quite new to Racket.

另外,如果只给出 xy 的值(在程序)?

Also, how would I go about retrieving the values if just given the values of x and y (within the program)?

例如,假设我能够以某种方式定义以下内容:

For example, let's say that somehow I was able to define the following:

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

(define x 10)
(define y 20)

有没有办法让我编写 variable-xy 并返回 some-value"?

Is there a way for me to write variable-x-y and get back "some-value"?

编辑 2这是我尝试实现的简化代码.它的作用是递归地将每个单独的元素读入一个局部变量,然后可以在全部读入"后使用该变量.我确定您是否使用找到的方法调整代码 here 它应该可以正常工作.

EDIT 2 Here is the simplified code of what I'm trying to implement. What it does is it recursively reads each individual element into a local variable which can then be used after it's all been "read in". I'm sure if you tweak the code using the method found here it should work just fine.

(define (matrix->variables matrix)
  (local [(define (matrix2->variables/acc matrix2 x y)
            (cond
              [;; the entire matrix has "extracted" it's elements into variables
               (empty? matrix2)
               #|This is where the main program goes for using the variables|#]
              [;; the first row has "extracted" it's elements into variables
               (empty? (first matrix2))
               (matrix2->variables/acc (rest matrix2) 0 (add1 y))]
              [else (local [(define element-x-y "some-value")]
                      ;; Here is where I got stuck since I couldn't find a way to
                      ;; name the 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 you create a variable name, you have to know how to call it, you can't define names dynamically.

也许用于存储绑定的哈希表会很有用,它有点类似于您所要求的并模拟具有动态定义的变量 - 但我仍然不确定为什么您想要这样做,听起来对我来说更像是 XY 问题.试试这个:

Perhaps a hash table for storing bindings will be useful, it's somewhat similar to what you ask and simulates having dynamically defined variables - but still I'm not sure why you want to do this, sounds more like an XY problem to me. Try this:

(define (create-key var1 var2)
  (string->symbol
   (string-append 
    "variable-"
    (number->string var1)
    "-"
    (number->string var2))))

; create a new "variable"
(define x 10)
(define y 20)
(create-key x y)
=> 'variable-10-20

; use a hash for storing "variables"
(define vars (make-hash))

; add a new "variable" to hash
(hash-set! vars (create-key x y) "some-value")

; retrieve the "variable" value from hash
(hash-ref vars 'variable-10-20)
=> "some-value"

这篇关于使用 Racket 中的变量命名变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆