3 ms·
In general if you're making an alist in Lisp then you're going to use keys for which comparisons are cheap; you would use atoms instead of strings as the keys:
by db48x 10y ago
In general if you're making an alist in Lisp then you're going to use keys for which comparisons are cheap; you would use atoms instead of strings as the keys: '((name "Jane") ...) instead of '(("name" "Jane") ...).
Strings must be compared byte-by-byte, atoms are interned and compared by pointer identity.
In fact, in some Lisps (such as Common Lisp) you would probably use a special type of atom called a keyword. Keywords are just atoms with a colon at the front to indicate that they belong to the special KEYWORD package; code in two different packages can then compare them without any namespacing fuss. It would look like '((:name "Jane") ...).
Lisp just takes the logic out of the syntax and puts it into the semantics of the language.
- lispm 10y agoWhat you call 'atom' is actually called 'symbol' in Lisp. Keywords are in Common Lisp a subset of symbols, those who are in the package 'KEYWORD'. Keywords also have themselves as value. Thus keywords evaluate to themselves. 'atom' means something else: 'not cons'. Anything that is not a cons cell (the two-pointer building block of linked lists) is an atom: numbers, characters, strings, symbols, arrays, ... Thus a string is an atom, too. CL-USER 1 > (typep "foobar" 'atom) T CL-USER 2 > (typep 4 'atom) T CL-USER 3 > (typep 'foobar 'atom) T CL-USER 4 > (typep '(foo . bar) 'atom) NIL True though, symbols (not atoms) are often used as keys for key/value data structures like assoc lists, property lists, hash tables, CLOS instances and others. Common Lisp also allows symbols to have arbitrary names. It uses | and \ as escape characters in symbols. CL-USER 11 > '((person |Marvin Minsky|) (|KNOWN FOR| |Artificial Intelligence Research|) (lab |MIT AI Lab|)) ((PERSON |Marvin Minsky|) (KNOWN\ FOR |Artificial Intelligence Research|) (LAB |MIT AI Lab|)) CL-USER 12 > (setf mm *) ((PERSON |Marvin Minsky|) (KNOWN\ FOR |Artificial Intelligence Research|) (LAB |MIT AI Lab|)) CL-USER 13 > (assoc '|KNOWN FOR| mm) (KNOWN\ FOR |Artificial Intelligence Research|) Symbols are by default interned in a special data structure and are looked up at read-time. Thus they are compared by pointer identity. In Common Lisp this data structure is called a package and there can be more than one package.
- db48x 10y agoYes, "symbol" is the correct term. Must have been a brain fart.