What is a symbol in a lisp / schema?

For the omnipotent love, I still have to understand the purpose of the symbol 'iamasymbol . I understand numbers, booleans, strings ... variables. But the symbols are too big for my little imperative thinking. Why am I using them? How should they be used in the program? My understanding of this concept simply fails.

+27
lisp scheme racket
Jan 13 2018-12-12T00:
source share
5 answers

In the schema and Racket, a character is like an immutable string that turns out to be interned, so that characters can be compared with eq? (fast, essentially a comparison with a pointer). Characters and strings are separate data types.

One use of characters is easy enumerations. For example, you can say that the direction is either 'north , 'south , 'east , or 'west . Of course, you can use strings for the same purpose, but it will be a little less efficient. Using numbers would be a bad idea; Present information as transparent and transparent as possible.

In another example, SXML is an XML representation using lists, characters, and strings. In particular, strings represent character data, and characters represent element names. Thus, the XML <em>hello world</em> will be represented by the value (list 'em "hello world") , which can be more compactly written '(em "hello world") .

Another use of characters is keys. For example, you can implement a method table as dictionary display characters for implementation functions. To call a method, you look at the character corresponding to the method name. Lisp / Scheme / Racket does this very simply because the language already has a built-in correspondence between identifiers (part of the language syntax) and characters (values โ€‹โ€‹in the language). This correspondence makes it easier to support macros that implement custom syntax extensions for the language. For example, you can implement a class system in the form of a macro library using the implicit correspondence between "method names" (a syntactical concept defined by a class system) and symbols:

 (send obj meth arg1 arg2) => (apply (lookup-method obj 'meth) obj (list arg1 arg2)) 

(In other Lisps, what I said is mostly true, but there are additional things to be aware of, such as packages and functions with variable slots, IIRC.)

+23
Jan 13 '12 at 8:29
source share

A symbol is an object with a simple string representation, which (by default) is guaranteed to be interned ; that is, any two characters that are written the same way are the same object in memory (referential equality).

Why do Lisps have characters? Well, this is pretty much an artifact of Lisps embedding its own syntax as a language data type. Compilers and interpreters use symbols to represent identifiers in a program; Because Lisp allows you to represent program syntax as data, it provides characters because they are part of the representation.

What are they useful besides this? Well, a few things:

  • Lisp is commonly used to implement embedded domain languages. Many of the methods used to do this come from the compiler world, so the characters are useful here.
  • Macros in Common Lisp typically include character processing in more detail than this answer provides. (Although, in particular, generating unique identifiers for macro extensions requires the ability to generate a character that would never be equal to any other.)
  • Corrected enumeration types are better implemented as characters than strings, because characters can be compared by reference equality.
  • There are many data structures that you can build, where you can benefit from the use of characters and reference equality.
+22
Jan 13 '12 at 17:32
source share

Characters in lisp are human-readable identifiers. All of them are single. Therefore, if you declare "foo somewhere in your code, and then use" foo "again, it will point to the same place in memory.

Usage example: different symbols can represent different parts on a chessboard.

+6
Jan 13 '12 at 6:18
source share

A symbol is simply a special name for a value. The value can be any, but this symbol is used to indicate the same value each time, and this kind of thing is used for quick comparison. As you say, you are smart, they are like numeric constants in C, and that is how they are usually implemented (internally stored numbers).

+3
Jan 13 '12 at 6:16
source share

From the structure and interpretation of computer programs Second edition of Harold Abelson and Gerald Jay Sussman 1996:

To manipulate characters, we need a new element in our language: the ability to quote a data object. Suppose we want to build a list (ab). We cannot do this with (list a) because this expression builds a list of values โ€‹โ€‹a and b, not the characters themselves. This question is well known in the context of natural languages, where words and sentences can be considered as semantic entities or as a line character (syntactic objects). A common practice in natural languages โ€‹โ€‹is to use quotation marks to indicate that you need to treat a word or sentence literally as a string of characters. For example, the first letter "John" is clearly "J." If we tell someone to say their name out loud, we expect that person to hear the name. However, if we tell someone to โ€œsayโ€ your name out loud, we expect to hear the words โ€œyour nameโ€. Note that we are forced to nest quotation marks to describe what someone can say. We can follow the same practice to identify lists and characters that are to be processed as data objects, and not as expressions to be evaluated. However, our citation format differs from the natural language format in that we put a quote (traditionally, a single quote character) only at the beginning of the object for citation. We can get away from this in Schema syntax because we rely on spaces and parentheses to delimit objects. Therefore, the meaning of the single quote character is to quote the following object. Now we can distinguish between characters and their meanings:

 (define a 1) (define b 2) (list ab) (1 2) (list 'a 'b) (ab) (list 'ab) (a 2) 

Lists containing characters may look like expressions in our language:

 (* (+ 23 45) (+ x 9)) (define (fact n) (if (= n 1) 1 (* n (fact (- n 1))))) 

Example: Symbolic Differentiation

0
Sep 23 '17 at 15:51
source share



All Articles