[plt-scheme] The role of proper and improper lists?

From: Aleks Bromfield (aleks at cs.brown.edu)
Date: Thu Jan 24 23:37:24 EST 2008

On Thu, Jan 24, 2008 at 09:24:07PM -0600, Grant Rettke wrote:
> Suppose you got a list of key-pair values. You could assemble it, for
> example, in one of the two ways:
> (list (list 'a 1) (list 'b 2) (list 'b 3))
> or
> (list (cons 'a 1) (cons 'b 2) (cons 'c 3))

Here's a third way:

  (define-struct kvpair (key value))
  (list (make-kvpair 'a 1) (make-kvpair 'b 2) (make-kvpair 'c 3))

Lists hold an indefinite amount of homogeneous data; structs hold a fixed amount
of heterogeneous data.

It's possible to use Scheme lists as if they were structures, but it's not a
good idea. The more structured your data is, the easier it is to work with, and
the easier it will be to understand your code at a later point in time.

As far as improper pairs go, avoid the temptation. It'll save you a bit of
typing, but you're much better off defining a struct.

~ Aleks

Posted on the users mailing list.