在Clojure中,如何定义一个由字符串命名的变量? [英] In Clojure, how to define a variable named by a string?

查看:99
本文介绍了在Clojure中,如何定义一个由字符串命名的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我试过这个:

 (doall(for [x [abc]](def(symbol x)666)))

...但这会产生错误


java.lang.Exception:def的第一个参数必须是一个符号


任何人都可以告诉我正确的方法来完成这个?

解决方案

Clojure的intern函数用于此目的:

 (doseq [x [abc]] 
(intern * ns *(symbol x)666))


Given a list of names for variables, I want to set those variables to an expression.

I tried this:

(doall (for [x ["a" "b" "c"]] (def (symbol x) 666)))

...but this yields the error

java.lang.Exception: First argument to def must be a Symbol

Can anyone show me the right way to accomplish this, please?

解决方案

Clojure's "intern" function is for this purpose:

(doseq [x ["a" "b" "c"]]
  (intern *ns* (symbol x) 666))

这篇关于在Clojure中,如何定义一个由字符串命名的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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