Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Clojure have "keywords" in addition to "symbols"?

I have a passing knowledge of other Lisps (particularly Scheme) from way back. Recently I've been reading about Clojure. I see that it has both "symbols" and "keywords". Symbols I'm familiar with, but not with keywords.

Do other Lisps have keywords? How are keywords different from symbols other than having different notation (ie: colons)?

like image 223
Laurence Gonsalves Avatar asked Oct 06 '09 19:10

Laurence Gonsalves


People also ask

What are keywords in Clojure?

Keywords are symbolic identifiers that evaluate to themselves. They provide very fast equality tests... Symbols are identifiers that are normally used to refer to something else. They can be used in program forms to refer to function parameters, let bindings, class names and global vars...

What is a symbol in Clojure?

In Common Lisp, a "symbol" is a location in memory, a place where data can be stored. The "value" of a symbol is the data stored at that location in memory. In Clojure, a "symbol" is just a name. It has no value.

What is a namespace Clojure?

A namespace is both a name context and a container for vars. Namespace names are symbols where periods are used to separate namespace parts, such as clojure. string . By convention, namespace names are typically lower-case and use - to separate words, although this is not required.


2 Answers

Here's the Clojure documentation for Keywords and Symbols.

Keywords are symbolic identifiers that evaluate to themselves. They provide very fast equality tests...

Symbols are identifiers that are normally used to refer to something else. They can be used in program forms to refer to function parameters, let bindings, class names and global vars...

Keywords are generally used as lightweight "constant strings", e.g. for the keys of a hash-map or the dispatch values of a multimethod. Symbols are generally used to name variable and functions and it's less common to manipulate them as objects directly except in macros and such. But there's nothing stopping you from using a symbol everywhere you use a keyword (if you don't mind quoting them all the time).

The easiest way to see the difference is to read Keyword.java and Symbol.java in the Clojure source. There are a few obvious implementation differences. For example a Symbol in Clojure can have metadata and a Keyword can't.

In addition to single-colon syntax, you can use a double-colon to make a namespace-qualified keyword.

user> :foo :foo user> ::foo :user/foo 

Common Lisp has keywords, as do Ruby and other languages. They are slightly different in those languages of course. Some differences between Common Lisp keywords and Clojure keywords:

  1. Keywords in Clojure are not Symbols.

    user> (symbol? :foo)   false 
  2. Keywords don't belong to any namespace unless you specifically qualify them:

    user> (namespace :foo) nil user> (namespace ::foo) "user" 

(Thanks Rainer Joswig for giving me ideas of things to look at.)

like image 122
Brian Carper Avatar answered Nov 29 '22 14:11

Brian Carper


Common Lisp has keyword symbols.

Keywords are symbols, too.

(symbolp ':foo) -> T 

What makes keywords special:

  • :foo is parsed by the Common Lisp reader as the symbol keyword::foo
  • keywords evaluate to themselves: :foo -> :foo
  • the home package of keyword symbols is the KEYWORD package: keyword:foo -> :foo
  • keywords are exported from the package KEYWORD
  • keywords are constants, it is not allowed to assign a different value

Otherwise keywords are ordinary symbols. So keywords can name functions or have property lists.

Remember: in Common Lisp symbols belong to a package. This can be written as:

  • foo, when the symbol is accessible in the current package
  • foo:bar, when the symbol FOO is exported from the package BAR
  • foo::bar, when the symbol FOO is in the package BAR

For keyword symbols that means that :foo, keyword:foo and keyword::foo are all the same symbol. Thus the latter two notations are usually not used.

So :foo is just parsed to be in the package KEYWORD, assuming that giving no package name before the symbol name means by default the KEYWORD package.

like image 32
Rainer Joswig Avatar answered Nov 29 '22 15:11

Rainer Joswig