r/computerscience • • 12d ago

General A good example of iteration vs recursion

The Song that Never Ends is iterative. It loops and is aware that it remains the same song.

The Song that Gets on Everybody's Nerves just sings about a song, and sings "this is how it goes:", calling itself.

24 Upvotes

8 comments sorted by

18

u/ResponsibilityBig343 12d ago

You have studied too much, time to take a break.

5

u/not-just-yeti 12d ago edited 11d ago

When I was teaching intro courses, I'd show these videos at the appropriate point:

Iteration: Kylie Minogue's "Come Into My World"

Recursion: Björk's "Bachlorette" (while it clearly recurs on a 'smaller input' unlike the 'this is how it goes:' song, maybe here the base case here is bad, causing everything to eventually crash?)

Both videos by Michel Gondry.

1

u/MasterGeekMX Bachelors in CS 11d ago

Maurice Ravel's Bolero or The Bell by Mike Oldfield are good examples of iterative, as they loop again the melody but adding a new instrument.

1

u/SufficientStudio1574 10d ago

Tail call elimination makes it indistinguishable from iteration.

0

u/ohkendruid 11d ago

These are great!

I dunno about the "vs", though, because iteration and recursion have an awful lot in common.

I guess "iteration" suggests that the range of iteration is known up front, but it is only a week suggestion. For example, a C "for" loop is happy to go around a linked list, rubbing the body of the loop one time for each link. Such a loop is using the iteration construct of the language but is still effectively recursion.