adding elements to the end of the list

Discussion in 'Cadence' started by tattvamasi, May 12, 2006.

  1. tattvamasi

    tattvamasi 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
     
    tattvamasi, May 12, 2006
    #1
  2. tattvamasi

    S. Badel Guest

    given a list a and element b whats the fastest way to add the element
    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.
    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
     
    S. Badel, May 12, 2006
    #2
  3. tattvamasi

    tattvamasi Guest

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

    Partha
     
    tattvamasi, May 12, 2006
    #3
  4. 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, May 12, 2006
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.