adding elements to the end of the list

  • Thread starter tattvamasi@gmail.com
  • Start date
T

tattvamasi@gmail.com

Guest
given a list a and element b whats the fastest way to add the element
to the end of thel list?
tconc procedure does not work if the list is not built using the
structure?

one of the ways I can think of without iterating over the list is,
reverse(cons(b reverse(a))

Are there any faster methods? ( If at all the above is fast...)

Thanks,
Partha
 
given a list a and element b whats the fastest way to add the element
to the end of thel list?
tconc procedure does not work if the list is not built using the
structure?
but starting from a_list you may

t_list = lconc(nil a_list)

then joyfully use tconc as much as you like on t_list

and you don't even have to take the car of t_list as this destructively modifies a_list.

one of the ways I can think of without iterating over the list is,
reverse(cons(b reverse(a))
I tested for the record, and this is even slower than append.

tconc is definitely much, much faster than these, and the cost of calling lconc to build the tconc
structure is negligible.


stéphane
 
Stephane,
Thanks, I knew there had to be a better way!

Partha
 
On Fri, 12 May 2006 11:25:48 +0200, "S. Badel"
<stephane.badel@REMOVETHISepfl.ch> wrote:

given a list a and element b whats the fastest way to add the element
to the end of thel list?
tconc procedure does not work if the list is not built using the
structure?

but starting from a_list you may

t_list = lconc(nil a_list)

then joyfully use tconc as much as you like on t_list

and you don't even have to take the car of t_list as this destructively modifies a_list.

one of the ways I can think of without iterating over the list is,
reverse(cons(b reverse(a))

I tested for the record, and this is even slower than append.

tconc is definitely much, much faster than these, and the cost of calling lconc to build the tconc
structure is negligible.


stéphane
I too would probably convert it to a tconc structure using lconc, but if not,
you could always use the destructive form of append - nconc() - this still has
to traverse the list to get to the end, but no copying is needed.

It really depends on how much you need to add to the end of the list.

profiling is a good idea to find the best solution if there is a reasonable
amount of data.

Andrew.
--
Andrew Beckett
Principal European Technology Leader
Cadence Design Systems, UK.
 

Welcome to EDABoard.com

Sponsor

Back
Top