Subject: Re: List vs Array
From: rpw3@rpw3.org (Rob Warnock)
Date: Sat, 31 May 2003 04:58:46 -0500
Newsgroups: comp.lang.lisp
Message-ID: <ZpecneVLXJhL4UWjXTWcoA@speakeasy.net>
Drew McDermott  <drew.dot.mcdermott@at.yale.dot.edu> wrote:
+---------------
| If I do need true random access with a subscript computed at run time, 
| then I do indeed use an array.  But even there the construction phase is 
| often separable from the access phase, and during construction it is 
| often more convenient to use a list, which gets converted to an array 
| when it's done.  The alternative would be to do some preprocessing to 
| figure out the size of the array, allocate it, then fill it in.  If I've 
| ever done that in Lisp, it was a long time ago.
+---------------

Well, one of the places the latter style does seem to come naturally is
when loading up an array from a file or stream, where you end up doing
things of this general pattern [which many Lisps handle very efficiently]:

	(defun suck-up-file (filename)
	  (with-open-file (stream filename)
	    (let* ((length (file-length stream))
		   (data (make-sequence 'string length))
		   (length-read (read-sequence data stream)))
	      (unless (= length-read length)
		(error "Incomplete read on ~S: read ~D, should be ~D"
		       stream length-read length))
	      data)))


-Rob

p.s. See also CMUCL's READ-N-BYTES...

-----
Rob Warnock, PP-ASEL-IA		<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607