1
votes

Why does http://www.lisperati.com/clojure-spels/data.html use quoted symbols, instead of keywords?

(def objects '(whiskey-bottle bucket frog chain))

(def game-map (hash-map
   'living-room '((you are in the living room
                   of a wizards house - there is a wizard
                   snoring loudly on the couch -)
                  (west door garden)
                  (upstairs stairway attic))
   'garden '((you are in a beautiful garden -
              there is a well in front of you -)
             (east door living-room))
   'attic '((you are in the attic of the
             wizards house - there is a giant
             welding torch in the corner -)
            (downstairs stairway living-room))))

(def object-locations (hash-map
                       'whiskey-bottle 'living-room
                       'bucket 'living-room
                       'chain 'garden
                       'frog 'garden))

(def location 'living-room)

(defn describe-location [location game-map]
  (first (location game-map)))

I changed 'living-room to :living-room in all places and it worked.

I'm very new to Clojure - what is the point of 'something, when :something is available to be used?

(I understand what quote does - I would like to know when to use a quoted symbol instead of a keyword.)

2
'something is a symbol whereas :something is a keyword. The classical lisp doesn't have concept of keywords. Your question title should be about symbol vs keyword, not quote vs symbol as quote is a way to create symbol - Ankur
Thanks, I was confused there. I will edit. - fadedbee
@Ankur What about Common Lisp :keywords?? - Sylwester

2 Answers

6
votes

The original version of Casting SPELs in Lisp was written for Common Lisp. While CL also has keywords (i.e. :foo), the author chose to use quoted lists of symbols instead. This captures the magic of Lisp, and the idea of "code as data" -- a symbol, when evaluated, can represent a value such as a function, but can also represent a value in and of itself, which is one of the cool things about Lisp.

This is just my impression, but when the author translated Casting SPELs for Clojure, I believe he made the decision to still use symbols to represent data (even though in Clojure it would be more idiomatic to use keywords and strings) in order to keep intact the underlying message about Lisp and being able to use code as data and data as code.

2
votes

Using symbols for things in the game makes it much easier to build an interface than if he choose to use keywords.

If you had read it through you see that you control the game by entering a command like (walk west). I think the author though that was way better than (walk :west) and it's probably much simpler to do it with symbols than converting west from the input into the keyword :west without confusing a beginner programmer. In both the original as well as your Clojure version Conrad fixes the interface with macros so you don't quote anything in the finished game and you don't need to use strange colons either.

Casting Spels is a draft/variation of chapter 5 of his book called The Land of LISP. In the book he don't use macros but creates a game read-eval-print loop. With a REPL you get everything in as data and thus don't need to quote anything there either when the game is finished.

The fact that he uses symbols instead of strings is strange until you think that he probably wants the reader to become intimate with symbols early on.

If you want to learn just a little CL I strongly recommend the book since it's even more hilarious than Casting SPELs.

Why don't you try as an exercise to use keywords and try to write the macro or perhaps a REPL so that you can still write walk west and that it does (walk-direction :west) without hadcoding the mapping between west and :west. Would it make the game more complex?