Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is a symbol in lisp/scheme?

For the love of the almighty I have yet to understand the purpose of the symbol 'iamasymbol. I understand numbers, booleans, strings... variables. But symbols are just too much for my little imperative-thinking mind to take. What exactly do I use them for? How are they supposed to be used in a program? My grasp of this concept is just fail.

like image 236
dotnetN00b Avatar asked Jan 13 '12 06:01

dotnetN00b


People also ask

What is symbol in LISP?

In LISP, a symbol is a name that represents data objects and interestingly it is also a data object. What makes symbols special is that they have a component called the property list, or plist.

What is a symbol in scheme?

Symbols in Scheme are widely used in three ways: as items of discrete data, as lookup keys for alists and hash tables, and to denote variable references. Looking beyond how they are written, symbols are different from strings in two important respects. The first important difference is uniqueness.

Is LISP a symbolic language?

lisp and WolframLang are said to be symbolic languages.

Why use LISP?

Seibel describes Lisp as a tool for getting “more done, faster”. Here, you can start to see why this is so. Lisp languages are immensely flexible and permissive in how their pieces can be connected. This means that the way you think about a programming problem can be quite close to the way you actually program it.


Video Answer


1 Answers

In Scheme and Racket, a symbol is like an immutable string that happens to be interned so that symbols can be compared with eq? (fast, essentially pointer comparison). Symbols and strings are separate data types.

One use for symbols is lightweight enumerations. For example, one might say a direction is either 'north, 'south, 'east, or 'west. You could of course use strings for the same purpose, but it would be slightly less efficient. Using numbers would be a bad idea; represent information in as obvious and transparent a manner as possible.

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

Another use for symbols is as keys. For example, you could implement a method table as a dictionary mapping symbols to implementation functions. To call a method, you look up the symbol that corresponds to the method name. Lisp/Scheme/Racket makes that really easy, because the language already has a built-in correspondence between identifiers (part of the language's syntax) and symbols (values in the language). That correspondence makes it easy to support macros, which implement user-defined syntactic extensions to the language. For example, one could implement a class system as a macro library, using the implicit correspondence between "method names" (a syntactic notion defined by the class system) and symbols:

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

(In other Lisps, what I've said is mostly truish, but there are additional things to know about, like packages and function vs variable slots, IIRC.)

like image 133
Ryan Culpepper Avatar answered Sep 25 '22 09:09

Ryan Culpepper