Thursday, 5 February 2015

Recursion Part 2: Spin off Idea?

In Recursion Part 1, I talked about one analogous recursive formulation in mathematics and the logic for using "simplified" tracing.
Here, I want to talk about my attempts to create a recursive function for the additional exercises. I took just the docstrings and thought about how to write it. The docstrings are given below:
From the docstring, I could guess that for the recursive structure: if recursive_case then base_case, the base_case should be False... but it was difficult to understand where the element comparison should be...

At the end, the solution that I reached was:
Here, all the provided test cases work... but it is obvious that it would fail for case nested_contains(2,2).
I personally think that this should be the correct response of the function considering an object as a list of depth 0, but I'm just trying to match the specification here... so... after consulting the TA's advice, I used a helper function that would call this structure:
This kind of got me to think of structures where function A calls B calls C calls A calls B and so forth. I haven't thought much about this problem yet, but it can obviously be used when you need some cyclic behavior until a certain point. A trivial example is given below:
A very useless thing but the number of function calls to A might be the solution to something like kola climbs 2 cm and then slips 1 cm every day; how many days does it take for the kola to travel to the top of a 100 cm tree.
Another possibility is to call something depending on what was called previously, but this is structured in such a way that the caller can be called in the "chain" of calls.

Recursion is really interesting, but thinking about it in this way makes it seem like just another function... with one caveat that it might "loop" infinitely.

No comments:

Post a Comment