[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: when to use strings, when to use symbols

From: Hans Aberg
Subject: Re: when to use strings, when to use symbols
Date: Sun, 9 Jan 2011 11:07:15 +0100

On 9 Jan 2011, at 03:25, Andy Wingo wrote:

Let's say you have an association list. It could be (("foo" . "bar")) or
((foo . "bar")) or ((foo bar)), or even (("foo" . bar)).  If you're
parsing this data from over the network, in textual form, which do you

The answer is this:

 * Use symbols when you want to match against a symbol literal in your
   source code.

 * Use strings otherwise.

I have found very useful the associative container with keys implemented as a balanced tree and called std::map in C++ and Map in Haskell. The keys then varies according to context. For example, sparse multivariate polynomials can be implemented as a map from monomials to coefficients. Then sparse multivariate monomials can be implemented as a map from variables to exponents (integers); for variables, I choose strings.

ps. I very, very rarely hold Python up as an example of language design, especially regarding strings, but I think that their approach to string
mutabililty and identity is preferable to the symbol vs string
distinction that Scheme has.

Symbols and strings are semantically different with respect to the lambda theory. Perhaps in Scheme, symbols are used in other contexts because 'eq?' is faster, which was important on the slower computers of the past.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]