Friday, 31 January 2014

Week 4 - Part 2:  Recursion, recursion, more recursion, and....turtles?

I got slightly confused at when we were supposed to write our week 3/4 slog entries so I ended up making two week 4 posts; so after this one I'll have covered everything until Jan 31st.

This week we delved into recursive functions with slightly more detail than we did previously.  In both this week's lectures, we did a recursion example in each.  The first was another recursive function involving nested lists and list comprehensions.  In the second, we were introduced to the ancient art of turtle drawing to help illustrate the different steps in the recursive function that was shown.

There really wasn't any spectacularly new content in the first example.  I think that Danny was just trying to emphasize how- when we trace recursive functions -  it's important to know when to 'stop' tracing. Ie, when we traced the behaviour of the function with a singly nested list, we could use that information when we traced a double/triple/etc nested list.  Just for future reference, here's the function from the course website:
Notice the + [0] in the list comprehension - it's to prevent an error when L is an empty list (you cannot take the max of an empty list).

The second example used turtles to visually trace what happens during a recursive function, which I actually found super helpful (since I'm a very visual learner).  Once again, here's the code:

If I set the level to 3, the turtles start drawing the following:
Notice that whenever a new branch is drawn, the following 'sub branch' to be drawn is always red; hence the function does not draw a three branched star from largest to smallest after each iteration of the for loop, but it starts at the tip of each vertex.

Because of fractal drawings, recursive functions have now become much more interesting/easy to understand since I can now see what's happening as the function executes.  I hope Danny uses more visuals like these in the future.

Since there was relatively little completely new content this week, this slog entry will be quite a bit shorter than my previous ones, which I think my TA (or whoever's marking this) will greatly appreciate.





No comments:

Post a Comment