16.5. 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.


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?


Discuss topics in this section with classmates.

Show Comments
You have attempted of activities on this page