在Clojure中,如何定义一个以字符串命名的变量? [英] In Clojure, how to define a variable named by a string?
本文介绍了在Clojure中,如何定义一个以字符串命名的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定变量名称列表,我想将这些变量设置为表达式.
Given a list of names for variables, I want to set those variables to an expression.
我试过了:
(doall (for [x ["a" "b" "c"]] (def (symbol x) 666)))
...但这会产生错误
java.lang.Exception: def 的第一个参数必须是 Symbol
java.lang.Exception: First argument to def must be a Symbol
谁能告诉我实现此目的的正确方法吗?
Can anyone show me the right way to accomplish this, please?
推荐答案
Clojure 的实习生"功能就是为了这个目的:
Clojure's "intern" function is for this purpose:
(doseq [x ["a" "b" "c"]]
(intern *ns* (symbol x) 666))
这篇关于在Clojure中,如何定义一个以字符串命名的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文