【发布时间】:2015-04-04 14:58:24
【问题描述】:
如果我有两个变量,例如
(define x 10)
(define y 20)
我想创建一个新变量,使用 x 和 y 的值来创建名字,我该怎么做?
例如,假设我想创建一个名为 variable-x-y
的新变量(define variable-x-y "some-value")
在这种情况下,x 为 10,y 为 20。
基本上总结一下,我希望能够输入 variable-10-20 并让它返回 "some-value"
如果这听起来像是一个新手问题,我很抱歉。我对 Racket 很陌生。
编辑: 另外,如果只给出 x 和 y 的值(在程序)?
例如,假设我能够以某种方式定义以下内容:
(define variable-10-20 "some-value")
(define x 10)
(define y 20)
有没有办法让我写 variable-x-y 并返回 "some-value" ?
编辑 2 这是我要实现的简化代码。它所做的是将每个单独的元素递归地读入一个局部变量,然后可以在它全部被“读入”后使用。我敢肯定,如果您使用 here 找到的方法调整代码,它应该可以正常工作。
(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)))
【问题讨论】:
-
另外,如果只给出变量 x 和 y,我将如何检索 variable-x-y 的值b>(在程序内)?我会更新问题以反映这一点。
-
如果不是因为我还不允许在我的程序中使用 define-syntax,这将是完美的。不管怎样,谢谢你指点我。
标签: scheme racket variable-assignment