concept previous chapter in category functional programming

appears as: previous chapters, previous chapter
Grokking Functional Programming

This is an excerpt from Manning's book Grokking Functional Programming.

It worked! haskell is at the top of the ranking, because its score is 6. There is one problem though. We mutate the existing words list inside the function! Note that sort method returns void making it obvious that it mutates the argument that we passed. This solution violates the rules of a pure function and invites all the problems we’ve discussed in previous chapters. We can do better, and do it in Java!

Recap: Statements vs Expressions

There is a distinction between a statement and an expression. A statement is a language construct that needs to change the state of a program in order to be useful. In Java, this doesn’t do anything:

if (bracketOpen != -1 && dash > bracketOpen + 1)   rawShow.substring(bracketOpen + 1, dash); else null;

An expression, on the other hand, always returns something, and, when executed many times, it always returns the same result—a value. In fact, we can even say that the following expression is a value:

val yearStrOpt = if (bracketOpen != -1 && dash > bracketOpen + 1)                    Some(rawShow.substring(bracketOpen + 1, dash))                  else None

THIS IS BIG!

FP is programming with expressions. Functional programmers don’t use statements.