unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* when to use strings, when to use symbols
@ 2011-01-09  2:25 Andy Wingo
  2011-01-09  9:23 ` Thien-Thi Nguyen
  2011-01-09 10:07 ` Hans Aberg
  0 siblings, 2 replies; 3+ messages in thread
From: Andy Wingo @ 2011-01-09  2:25 UTC (permalink / raw)
  To: guile-user

Hey all,

I have been going over the new web interface in Guile 2.0, making sure
that it's OK for long-term use, and got myself into a quandary about
when to use strings and when to use symbols in an interface.  I don't
pretend to have the right answer here, but this is the answer that I
have come to.

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
use?

The answer is this:

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

  * Use strings otherwise.

  * Don't worry about symbol-interning attacks, where the remote side
    fills up your symbol table -- symbols are properly garbage-collected
    in Guile 2.0.

So in this case, the answer is ((foo . "bar")) if you would ever do
(assoc-ref alist 'foo) in code, and (("foo" . "bar")) otherwise.

Cheers,

Andy

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.
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: when to use strings, when to use symbols
  2011-01-09  2:25 when to use strings, when to use symbols Andy Wingo
@ 2011-01-09  9:23 ` Thien-Thi Nguyen
  2011-01-09 10:07 ` Hans Aberg
  1 sibling, 0 replies; 3+ messages in thread
From: Thien-Thi Nguyen @ 2011-01-09  9:23 UTC (permalink / raw)
  To: guile-user

() Andy Wingo <wingo@pobox.com>
() Sat, 08 Jan 2011 18:25:32 -0800

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

     * Use strings otherwise.

Symbols are preferable whenever ‘eq?’ is important
(for whatever reason, depending on program design).
Many times that involves matching against a symbol
literal, but that's a specific case.

For example, if the accepted alist is intended for
key duplicates pruning, using a symbol in the key
means you can use ‘assq’ (i.e., ‘eq?’), which can
be (and usually is) more performant than ‘assoc’.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: when to use strings, when to use symbols
  2011-01-09  2:25 when to use strings, when to use symbols Andy Wingo
  2011-01-09  9:23 ` Thien-Thi Nguyen
@ 2011-01-09 10:07 ` Hans Aberg
  1 sibling, 0 replies; 3+ messages in thread
From: Hans Aberg @ 2011-01-09 10:07 UTC (permalink / raw)
  To: Andy Wingo; +Cc: guile-user

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
> use?
>
> 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.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-01-09 10:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-09  2:25 when to use strings, when to use symbols Andy Wingo
2011-01-09  9:23 ` Thien-Thi Nguyen
2011-01-09 10:07 ` Hans Aberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).