3 ms·
Why are the keywords written with a quote? > (keywords '(useless microframework academic swag)) Shouldn't it be a list with strings or something? What if the
by MichaelGG 14y ago
Why are the keywords written with a quote?
> (keywords '(useless microframework academic swag))
Shouldn't it be a list with strings or something? What if the keywords were complex structures not just strings?
- nialo 14y agoif it's not quoted the list will be treated as an expression, and evaluate "Useless", which is probably not what you want.
- MichaelGG 14y agoRight so instead you'd write: keywords (list "a" "b") ? Which would let you also do complex things like (list (person "bob") ...)
- deleted 14y ago[deleted]
- RaphiePS 14y agoSee my comment above. The core difference is that (list ...) evaluates what's inside. So (list (+ 1 2)) returns (list 3). ' doesn't evaluate anything. So '((+ 1 2)) returns '((+ 1 2)).
- RaphiePS 14y agoThe only Lisp I know is Racket (basically Scheme), so take the following with a grain of salt. '(foo bar baz) is almost the same thing as (list foo bar baz). However, ' also makes the contents of the list into symbols, not variable references. Basically, '(foo bar baz) returns (list 'foo 'bar 'baz), not a list containing the values of the variables foo, bar, and baz. Symbols aren't much like strings. They're immutable, and actually just refer to a unique number, not characters. I hope that sort of makes sense!
- nivertech 14y ago'(foo bar baz) is sugar for (quote (foo bar baz)) list function unlike quote will evaluate it's arguments, i.e. user=> (quote (a b c)) (a b c) user=> '(a b c) (a b c) user=> (list a b c) CompilerException java.lang.RuntimeException: Unable to resolve symbol: a in this context, compiling:(NO_SOURCE_PATH:1) user=> (list 'a 'b 'c) (a b c)
- randallsquared 14y ago> Symbols aren't much like strings. They're immutable, and actually just refer to a unique number, not characters. Depending on language, that could make them exactly like strings. :)
- derleth 14y agoIf you have immutable strings and a smart compiler, yes, you could indeed have strings and symbols that are functionally identical. In fact, in Old Lisp, there were no strings and symbols were used for everything we use strings for now; that lead to weak string handling and it's why Common Lisp and all modern Lisp variants have a string type.
- ScottBurson 14y agoYou're asking two questions here. First, about the quote. I'd say it's a mistake. The author hasn't been completely clear (and I think is possibly a little confused) over the difference between s-expressions in general, which are simply a tree-structured representation, and Lisp code, which is written as s-expressions but is subject to additional rules. In this case, it's pretty clear from the surrounding discussion that these s-expressions are not intended to be executable code. That being the case, there's no reason to use the quote. This line should have been > (keywords (list useless microframework academic swag)) Secondly, as others have mentioned, there is an important difference between symbols and strings that is not necessarily easy to explain -- indeed, it took the Lisp community a few decades to get clear on it. Fundamentally: strings are data; symbols are names. Notice, in this example, that all of the keys in the dict, as well as "dict" itself, are represented by symbols. That is because each of these names something in the domain of the program that manipulates the representation: an object type, an attribute of an object type, or, in the case you are asking about, a member of a set of keywords. If you've used enumeration types in a language that has them (C/C++, Java, C#, etc.), there's an analogy: symbols are like literals of a single, pre-existing enumeration type containing all possible names.