Re: Can't take the tail of a list without copying via List[1:]?

Aaron Watters (aaron@funcity.njit.edu)
Fri, 5 Aug 1994 13:14:35 GMT

In article <1994Aug5.120725.3095@njitgw.njit.edu> aaron@funcity.njit.edu (Aaron Watters) writes:
>
>I didn't see this in the faq or other docs.
>
>Summary: Can I or why can't I take the cdr of a list without
> copying the entire top level structure of the list?
>
>Discussion:
>In many applications it is quite common to construct big lists
>and recurse over their structure:
>
>def NeatStuff(List):
> Head,Tail = List[0],List[1:]
> if Head=1:
> GroovyStuff(Tail)
> elif Head=2:
> NastyStuff(Tail)
> else:
> NeatStuff(Tail)
>
>Unfortunately List[1:] makes a _copy_ of the top level structure of the
>rest of List, which is quite expensive for large lists if all I want
>is Common Lisp's (rest List) which gives me the rest of List without
>copying anything.
>
>Since I like to write code like this I'm actually tempted to
>reimplement my own lisp class, which seems a little silly. Am I
>missing something? If List.rest() could be easily added why not add
>it?
> curious
> Aaron Watters

Looking at the code I figured this out for myself.
Lists are allocated as arrays (presumedly for fast lookup
and efficient garbage collection). Unfortunately this
blows certain styles of programming out of the water.

All of life is a compromise!
Aaron Watters
Department of Computer and Information Sciences
New Jersey Institute of Technology
University Heights
Newark, NJ 07102
phone (201)596-2666
fax (201)596-5777
home phone (908)545-3367
email: aaron@vienna.njit.edu