Why recursion is so important

I was just writing a little program that should generate all possible combination out of a list of chars. In my brain something like this evolved:
 1: wl = ['a','b', 'c', 'd', 'e', 'f', '']
2: out = []
4: for i1 in set(wl):
5: for i2 in set(wl):
6: for i3 in set(wl):
7: for i4 in set(wl):
8: for i5 in set(wl):
9: for i6 in set(wl):
10: for i7 in set(wl):
11: types = i1+i2+i3+i4+i5+i6+i7
12: out.append(types)
But I think everyone can see that that was a stupid idea.
Saying that I have seen that in quite some projects. My next thought was to bring recursion into this.
 1: wl = ['a','b', 'c', 'd', 'e', 'f', ' ']
2: out = []
4: def recLoop(txt):
5: if len(txt) >= len(wl):
6: out.append(txt.replace(' ',''))
7: else:
8: for i in set(wl):
9: recLoop(txt + i)

This looks far nicer doesn't it. I hope everyone who programs can see this. Just to state how important recursion is and I don't really understand why unis don't teach this more.

No comments: