Clojure中的'()和(list)之间有什么区别? [英] What's the difference between '() and (list) in Clojure?

查看:135
本文介绍了Clojure中的'()和(list)之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

列表上的 API Cheatsheet 部分似乎表明'()是一个列表构造函数,就像(list),但我发现在实践中他们不完全一样。例如,给定:

The API Cheatsheet section on Lists seems to indicate that '() is a list constructor, just like (list), but I've found that in practice they're not exactly the same. For example, given:

(def foo "a")
(def bar "b")
(def zip "c")

以下语句:

(apply str '(foo bar zip))


$ b

produces the output "foobarzip", which I wouldn't expect.

但是假设的是等价的:

(apply str (list foo bar zip))

产生abc,如我所料。

produces "abc", as I'd expect.

这是怎么回事?如果在Clojure中有一个列表的简写(例如 {} 表示映射, [] 表示向量)是什么?

What's going on here? If there's a "shorthand" for a list in Clojure (like {} for maps and [] for vectors), what is it?

推荐答案

在lisps中,'

换句话说'(foo bar zip)创建一个包含符号 foo bar zip ; (list foo bar zip)创建一个包含 foo bar zip 。在第一种情况下, str 会将符号本身转换为字符串,然后将它们连接起来。

To put it another way '(foo bar zip) creates a list containing the symbols foo, bar, zip; while (list foo bar zip) creates a list containing the values of foo, bar, zip. In the first case, str will be converting the symbols themselves to strings and then concatenating them.

=> (def foo "a")
=> (type (first '(foo)))
clojure.lang.Symbol
=> (type (first (list foo))) 
java.lang.String

这篇关于Clojure中的'()和(list)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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