Reversing a ListΒΆ

We can use the looping ability of computers, plus the indexing ability, to manipulate lists in interesting and even surprising ways. In the below program, we use an accumulator, which is something that accumulates values. We start with the accumulator soFar set to an empty list – a list with nothing in it. As the program executes, it appends items from the source list into the soFar list.

Note

Notice the [ and ] around the value in the source at the current value of index. This is needed because you can only concatenate two lists together. Try removing the [ and ]. What error do you get?

Before you keep reading...

Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.

You have attempted 1 of 3 activities on this page