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.)
Ryan Culpepper Jan 13 '12 at 8:29 2012-01-13 08:29
source share