Lisp and Erlang Atoms, Ruby and Scheme Symbols. How useful are they?

前端 未结 13 1602
一个人的身影
一个人的身影 2020-12-12 18:08

How useful is the feature of having an atom data type in a programming language?

A few programming languages have the concept of atom or symbol to represent a consta

13条回答
  •  隐瞒了意图╮
    2020-12-12 18:27

    In Lisp symbol and atom are two different and unrelated concepts.

    Usually in Lisp an ATOM is not a specific data type. It is a short hand for NOT CONS.

    (defun atom (item)
      (not (consp item)))
    

    Also the type ATOM is the same as the type (NOT CONS).

    Anything that is not a cons cell is an atom in Common Lisp.

    A SYMBOL is a specific datatype.

    A symbol is an object with a name and identity. A symbol can be interned in a package. A symbol can have a value, a function and a property list.

    CL-USER 49 > (describe 'FOO)
    
    FOO is a SYMBOL
    NAME          "FOO"
    VALUE         #
    FUNCTION      #
    PLIST         NIL
    PACKAGE       #
    

    In Lisp source code the identifiers for variables, functions, classes and so on are written as symbols. If a Lisp s-expression is read by the reader, it does create new symbols if they are not known (available in the current package) or reuses an existing symbol (if it is available in the current package. If the Lisp reader reads a list like

    (snow snow)
    

    then it creates a list of two cons cells. The CAR of each cons cell point to the same symbol snow. There is only one symbol for it in the Lisp memory.

    Also note that the plist (the property list) of a symbol can store additional meta information for a symbol. This could be the author, a source location, etc. The user can also use this feature in his/her programs.

提交回复
热议问题