• 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. Allegro X PCB Editor
  3. Swapping elements in a list

Stats

  • Replies 2
  • Subscribers 159
  • Views 13790
  • Members are here 0
More Content

Swapping elements in a list

EvanShultz
EvanShultz over 9 years ago

I need to receive a list and then reverse two elements in the list. Not finding a good way to do it with the included SKILL functions (though I could have just missed it), I wrote my own. Here it is (as usual, sorry for the lack of formatting):

; swaps element at given index with following element in list
; parameters:
; swap_index: index of film to move [integer]
; input_list: list to be modified [list]
; returns: input_list with swapped elements [list]
procedure(swap_next(swap_index input_list)
   let((j final_list)
      setq(j 0) ; need external counter in for loop variable auto-increments

      ; run through input list and either grab the current element or swap the next two
      for(i 0 (length(input_list) - 2)
         if(j == swap_index then
            ; at the index to swap, dump next element and then current element into output list
            final_list = cons(nth((j + 1) input_list) final_list)
            final_list = cons(nth(j input_list) final_list)

            j++ ; increment counter since we grabbed two elements
         else
            ; for any other index, just copy the input list to output list
            final_list = cons(nth(j input_list) final_list)
         )

         j++
      )

      reverse(final_list) ; return reversed list since it was built "backwards"
   )
)

This works, but it's awfully complicated. Since I don't have direct access to the auto-incrementing loop variable, I created my own. It seemed more complicated to use foreach() than this, but I'm a hardware guy who likes to think he can write a bit of code. I wouldn't be surprised if there's an obvious way to do it and I'm just daft for overlooking it.

Thanks in advance for any advice.

  • Sign in to reply
  • Cancel
  • eDave
    eDave over 9 years ago

    I think this would be better processed as an array (maybe use listToVector) but this might work for you:

    nconc(reverse(constar(nth(swap_index, input_list), nth(swap_index + 1, input_list), nthcdr(length(lst) - swap_index + 1, reverse(input_list)))) nthcdr(swap_index + 2, input_list))

    Using an array:

    v = listToVector(input_list)
    x = v[swap_index]
    v[swap_index] = v[swap_index + 1]
    v[swap_index + 1] = x
    vectorToList(v)

    A Lisp expert might me able to provide more elegant solutions.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • jwhend
    jwhend over 9 years ago

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Cadence Guidelines

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