algorithm

This is a page from my book, Functional Programming, Simplified

Tail-Recursive Algorithms in Scala

“Tail recursion is its own reward.”

From the “Functional” cartoon on xkcd.com.

Goals

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).

This is a page from my book, Functional Programming, Simplified

Recursion: Thinking Recursively

“To understand recursion,
one must first understand recursion.”

Stephen Hawking

Goal

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.

This is a page from my book, Functional Programming, Simplified

Functional Programming is Like Unix Pipelines

“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.”

Alfred Aho, one of the creators of the AWK programming language, in the book, Masterminds of Programming

Take time to think(!)

As a note to my future self: Take time to think!

*sigh*

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: