“Two steps are required to write a good piece of code. The first step is to get the algorithm right. The second step is to figure out which sorts of things (types) it works for.”
From the “Deriving a Generic Algorithm” chapter in the book, From Mathematics to Generic Programming.
“Tail recursion is its own reward.”
The main goal of this lesson is to solve the problem shown in the previous lessons: Simple recursion creates a series of stack frames, and for algorithms that require deep levels of recursion, this creates a
StackOverflowError (and crashes your program).
“To understand recursion,
one must first understand recursion.”
This lesson has one primary goal: to show that the thought process followed in writing the
sum function follows a common recursive programming “pattern.” Indeed, when you write recursive functions you’ll generally follow the three-step process shown in this lesson.
“Pipes facilitated function composition on the command line. You could take an input, perform some transformation on it, and then pipe the output into another program. This provided a very powerful way of quickly creating new functionality with simple composition of programs. People started thinking how to solve problems along these lines.”
As a note to my future self: Take time to think!
Even at my advanced age, if I don’t think through an algorithm I can still waste an awful lot of time.
As an example I just started working on a complex algorithm for my Android football game based on the initial thoughts in my brain, and came to regret it. After recovering from that faux-pas I decided to write just a few simple notes like this to clarify my thoughts:
“Perhaps the most important principle for the good algorithm designer is to refuse to be content.”
~ Alfred Aho
“A visual guide to graph traversal algorithms” is a fun website. As their docs state, “You can draw a new node by clicking anywhere on the visualization canvas. To add an edge between nodes, drag your mouse from one node to another.”
A couple of days ago I received an email about these guided meditation practices on Michael W. Taft’s TheMindfulGeek.com website. I’ve only listened to the first two so far, but if you’re interested in meditation, they can be a good resource.
Note: The code below is based on the Wikipedia formula, which is not correct. The correct algorithm seems to be at this page. I’ll update the code below once I verify this for sure.
In working on my “XO Play” Android football game, I just created this Java method to calculate the NFL Passer Rating for my quarterbacks: