• Skip to main content
  • Skip to search
  • Skip to footer
Cadence Home
  • This search text may be transcribed, used, stored, or accessed by our third-party service providers per our Cookie Policy and Privacy Policy.

  1. Community Forums
  2. Custom IC SKILL
  3. Remove last element from the list

Stats

  • Locked Locked
  • Replies 2
  • Subscribers 143
  • Views 18154
  • Members are here 0
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Remove last element from the list

jfatehi
jfatehi over 15 years ago

 I have a list A = '(1 2 3 4 5 6)

and I want the list  return without the last element. Any easy solution without going into looping?

  • Cancel
  • babji
    babji over 15 years ago

     To Start with  reverse(cdr(reverse(A)))

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel
  • Andrew Beckett
    Andrew Beckett over 15 years ago

    Generally speaking if you find yourself needing to remove the last element in a list, it may be your data structure is not well chosen. Perhaps it would be better to keep the list in reverse, and then you can easily and cheaply knock off the first entry in the list. Remember that lists are sequential - to do anything at the end, you have to get to the end by starting at the beginning of the list. Some functions (like tconc) get around this by maintaining a pointer to the last element of the list (so they can efficiently add onto the end of the list), but to remove the last entry in the list, you actually need a pointer to the last but one.

    If there was some sort of hybrid of mapcan and map, then this could be done with a simple and compact loop, but unfortunately there's not one built-in - it would have been foreach(mapDooDah rem A cdr(rem)&&list(car(rem))) . In the absence of that:

    procedure(MYremoveLast(lst)
      let((new)
        foreach(map rem lst
          when(cdr(rem) new=tconc(new car(rem)))
        )
        car(new)
      )
    )

    This has the benefit of only traversing the list once,  and uses less memory for a very big list than the double-reverse approach (which uses roughly twice the memory, and so for a big list can spend a lot of time in gc()). That said, the double-reverse approach is faster if you have enough memory to avoid needing to allocate more - it was 0.3 seconds rather than 2.3 seconds for a 5 million entry list. As soon as you don't have enough memory to store the new list, and it needs to keep allocating more though, the MYreverseList becomes much faster.

    Overall though, this is probably the wrong way to approach your problem, as I mentioned earlier.

    Regards,

    Andrew.

     

    • Cancel
    • Vote Up 0 Vote Down
    • Cancel

Community Guidelines

The Cadence Design Communities support Cadence users and technologists interacting to exchange ideas, news, technical information, and best practices to solve problems and get the most from Cadence technology. The community is open to everyone, and to provide the most value, we require participants to follow our Community Guidelines that facilitate a quality exchange of ideas and information. By accessing, contributing, using or downloading any materials from the site, you agree to be bound by the full Community Guidelines.

© 2025 Cadence Design Systems, Inc. All Rights Reserved.

  • Terms of Use
  • Privacy
  • Cookie Policy
  • US Trademarks
  • Do Not Sell or Share My Personal Information