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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Jan 25 09:30:52 EST 2008

On Jan 25, 2008, at 9:16 AM, Stephen De Gabrielle wrote:

> Good point,
> and where structs are insufficient use objects.
>
> what I struggle with is *when* to change from list to struct to  
> object?


I go from structs to objects when I have several functions of the shape

  ;;  f : StructOfFoo .. Other Types ... -> ...

and I wish to present this code to students past their first year or  
maintain it for a while.

I stick with structs when first-year students may look at my code.

I never replace lists with structs or objects. -- Matthias









>
>
> On 1/25/08, Aleks Bromfield <aleks at cs.brown.edu> wrote:
>> 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
>>
>> _________________________________________________
>>   For list-related administrative tasks:
>>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>
> -- 
> Sent from Google Mail for mobile | mobile.google.com
>
> Cheers,
>
> Stephen
>
> --
> Stephen De Gabrielle
> s.degabrielle at ucl.ac.uk
> Telephone +44 (0)20 7679 5242 (x45242)
> Mobile                  079 851 890 45
> Project: Making Sense of Information (MaSI)
> http://www.uclic.ucl.ac.uk/annb/MaSI.html
>
> UCLIC: University College London Interaction Centre
> http://www.uclic.ucl.ac.uk/
>
> Remax House - 31/32 Alfred Place
> London - WC1E 7DP
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.