Comments by "LoneTech" (@0LoneTech) on "Recursion 'Super Power' (in Python) - Computerphile" video.
-
8
-
6
-
3
-
2
-
@Mad_Elf_0 I want to treat it as impossible, because the number of discs is a non-negative integer. It's just not part of the problem domain, and in a statically typed language, shouldn't be a possible call. It's just as helpful as passing a non-numeric object. The problem operates in discrete non-negative integer space and functions correctly if and only if it reaches zero for all its reductions. If your program reaches an impossible state, failing as early as possible (typically, user input validation) helps pinpoint why. In the interest of failing early, it would make sense to assert that the number is a non-negative integer, but it would not help to make a subset of invalid values appear valid.
This is like someone telling you to solve the puzzle with nothing but a ball. There are no discs or pegs; you can't perform the task. You're proposing that "I did it!" is the proper response.
Adding a successful return of your function when called with bad input is removing the option for the call site to handle the problem, because you never alerted them. This is why "except: pass" is generally a very bad idea; you don't know why there was an exception (or even that there was one or which), and you've removed the opportunity for someone else to interpret it. An example of one that should generally not be silenced, certainly not in a loop, is KeyboardInterrupt - the user has told you to stop, probably for real world reasons your program doesn't understand.
PEP 20, the Zen of Python, captures this as "Errors should never pass silently." You should run "import this" from time to time.
2
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
@artit91 That's almost fully mistaken. The stack is the simplest, most fundamental form of dynamic memory allocation; it uses one pointer, with one side being free memory and the other occupied. alloca() can therefore just change the stack pointer, relying on the frame pointer to restore it, while functions not using dynamic allocation on the stack (like alloca or dynamically sized automatic arrays) can outright omit the frame pointer.
malloc's heap allocation, by contrast, requires tracking at least two pieces of information per chunk of memory; size and if it's free. Added to this is often a heap size which may or may not be dynamic (program break). On top of that it requires the appropriate search algorithms to transfer, split and merge chunks, and will handle fragmentation (when you can't allocate a size that is free, because the free memory is divided by allocated memory). The advantage is that you can free things in arbitrary order.
For machines without an MMU, stack placement is trivial; you place static allocations at one end of memory and start the stack pointer at one end of the remaining free space. The heap is frequently statically sized, such that a stack exhaustion is when the stack and heap meet. Often it is assumed that they never will meet, so there's no overhead checking for it, which an MMU would otherwise assist with.
The stack is so simple and fundamental it exists even on some computers that don't support indirect memory addressing in the first place, like PIC10.
What does typically complicate matters is multithreading, when you need more than one stack. That's why we have calls like pthread_attr_setstacksize, so you can allocate a suitable size for threads depending on their task.
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1
-
1