What's the difference between quote and string in this case with Clojure?
The following two commands prints out the same thing in repl:
user=> (println "(foo bar)")
(foo bar)
nil
user=> (println 开发者_JAVA百科(quote (foo bar))
(foo bar)
nil
So in this case, what's the difference between a quote and a string?
Edit:
(+ 3 2) and (+ (quote 3) 2)
are the same. The docs say quote yields the unevaluated form (so maybe I'm answering my own question here but please verify) that a quote is an optimization with lazy evaluation?
They're indeed different things:
user=> (class '(foo bar))
clojure.lang.PersistentList
user=> (class "foo bar")
java.lang.String
Even if they might have an identical println
result, they're not the same.
For the rest, @bmillare is right: you don't quote
for laziness, you quote to express literals.
The reason they look the same is because println is specified to print the content of strings and quoted forms, including the name of the symbols, to stdout. If you want to print the forms as how they would look like when inputted to the reader, use prn (pr if you don't want the newline)
user=> (prn "(foo bar)")
"(foo bar)"
nil
user=> (prn (quote (foo bar)))
(foo bar)
nil
For the other question,
Quote is not an optimization with lazy evaluation. The reason you get (+ 3 2) and (+ (quote 3) 2) is that you are quoting a literal e.g. a number, a keyword, or a string. (http://clojure.org/reader) Literals are evaluated at read time, before the form is passed to the upper form. Another way to think of it is quoting literals simply is defined to be an identity.
精彩评论