Functional algorithms for everyday problems
Now that we have seen modules, functions, guards, the basic types of the previous chapter, and some basic examples of pattern matching, we essentially have everything we need to start solving problems. Let's take this further and actually write some code!
Let's solve some basic problems such as those you might find in your standard set of interview questions; however, instead of solving them with imperative code, we are going to see how we can solve them using only functional constructs and what we have covered so far.
Iteration versus recursion
Often in functional languages, we will use recursion instead of iteration since iteration, by its very nature, requires side-effects. That is, to use a for
loop, most languages require that the loop modifies some state (usually, an integer) to keep track of where the loop is in execution.
Functional languages, contrastingly, opt for recursive strategies since these are (or, at the very least, can be) inherently...