vinson@linc.cis.upenn.edu (Jack Vinson) (05/30/91)
Me again, I am also playing with arrays for the first time in lisp and am wondering what kind of interesting functions there are for messing with them. In particular I will be frequently updating one array by moving elements down into the array. ie if the array is (1.2 1.1 1.2 1.3 1.1 1.0) and new data is 1.2 I want to put the new info at the 'beginning' of the array and shift everything down, so I would get (1.2 1.2 1.1 1.2 1.3 1.1) with the last element knocked off. The way I know how to do this is with a do-loop that copies element i into i+1, starting from the highest element. One solution is to just use a gigantic array which I just add new data to the end of, but this seems like it might waste space ~time~. Which is more expensive, keeping track of five 20x2 arrays the above way or creating five 5000x2 arrays and adding data from the beginning? Jack Jack Vinson vinson@linc.cis.upenn.edu
chucko@ptolemy.arc.nasa.gov (Chuck Fry) (05/31/91)
In article <43929@netnews.upenn.edu> vinson@linc.cis.upenn.edu (Jack Vinson) writes: >Me again, I am also playing with arrays for the first time in lisp and am >wondering what kind of interesting functions there are for messing with them. >In particular I will be frequently updating one array by moving elements down >into the array. ie if the array is (1.2 1.1 1.2 1.3 1.1 1.0) and new data is >1.2 I want to put the new info at the 'beginning' of the array and shift >everything down, so I would get (1.2 1.2 1.1 1.2 1.3 1.1) with the last element >knocked off. The way I know how to do this is with a do-loop that copies >element i into i+1, starting from the highest element. CLtL provides a facility for dealing with vectors (simple arrays) as stacks. If you don't mind the nth element of the array being most recent instead of the 0th, you can use VECTOR-PUSH or VECTOR-PUSH-EXTEND on an array with a fill-pointer. VECTOR-PUSH-EXTEND adjusts the array if the initial array size is exceeded, providing you created the array with :ADJUSTABLE T to begin with. Then VECTOR-POP can destructively access the most recent element, or (AREF stack (1- (FILL-POINTER stack))) will do the same thing non-destructively. If the most recent must be the 0th element, you're pretty much stuck with the DO-loop approach. Chuck Fry Chucko@Charon.ARC.NASA.GOV ...ames!ptolemy!chucko Disclaimer: No one but me is responsible for this misinformation. "Sun is in an ideal situation. It has convinced the market that open technology is the best. Yet, the company narrows its own interpretation of 'open' as its market share and technology advantages expands." -- "The Fifth Architecture", Mark Hall, in SunWorld, April 1991
halvers@altair.crd.ge.com (Pete Halverson) (05/31/91)
In article <13228@ptolemy-ri.arc.nasa.gov> chucko@ptolemy.arc.nasa.gov (Chuck Fry) writes: >In article <43929@netnews.upenn.edu> vinson@linc.cis.upenn.edu (Jack Vinson) writes: >>Me again, I am also playing with arrays for the first time in lisp and am >>wondering what kind of interesting functions there are for messing with them. >>In particular I will be frequently updating one array by moving elements down >>into the array. ie if the array is (1.2 1.1 1.2 1.3 1.1 1.0) and new data is >>1.2 I want to put the new info at the 'beginning' of the array and shift >>everything down, so I would get (1.2 1.2 1.1 1.2 1.3 1.1) with the last >>element knocked off. The way I know how to do this is with a do-loop >>that copies element i into i+1, starting from the highest element. > >CLtL provides a facility for dealing with vectors (simple arrays) as >stacks [using VECTOR-PUSH and VECTOR-POP]. >If the most recent must be the 0th element, you're pretty much stuck >with the DO-loop approach. You can also use the REPLACE function to shift stuff around, e.g. (replace my-array my-array :start1 1) should behave the same as (loop for i from (1- (length my-array)) downto 1 do (setf (aref my-array i) (aref my-array (1- i)))) and is hopefully (?) better optimized. Pete -- =============================================================================== Pete Halverson INET: halverson@crd.ge.com GE Corporate R&D Center UUCP: uunet!crd.ge.com!halverson Schenectady, NY