(Draft) Functional Programming
Best entry video
Functional Programming in 40 Minutes • Russ Olsen • GOTO 2018
You can find the slide here.
Mindset
 We don’t need to put our knowledge aside when learning functional programming.
 It’s more of a refactoring.
Functions in mathmatics
 It’s just maps from set to set.
 Functions donn’t compute somthing in math. It just is. A thing.
 Once the function was defined, the inputoutput correspondence never changes by other factors, which means, there is no side effects.
Borrow the concept from mathmatics
Programmers borrow the comcept of the functions from mathmetics.
 Make all of the data structures immutable. To do so, we can easily follow the complex code base. (it’s radical solution.)
 What if you want to mutate the value? > copy and change (create new data)
 How to reduce a lot of copy > copy&store only differences ~ persistent data structure (like copy on write, copy on modification)
 Wikipedia on this topic Techniques for preserving previous versions
Of course, immutable datastructures are not bad idea, but all our daily works (the customer cares) are side effects (mutate)! So, we need a bridge between pure functional idea and real “dirty” world.
Mutable state in FP (Not fully understand as of 15.Jan.2021)
An inpure FP language allows mutable directly. And in pure FP language, we can mutate with the concept like monad.
 In Clojure, it is solved by “Atoms”.
 For database operation or REST API, Clojure use Agents/Actors.
 Most of those solutions are like “putting the functions in a queue (the queue is the “dirty” world), and execute sequentially”.
FPI is not a magic.
Monad
I reviewed monad after I learned Option
in Rust, and after than, everything was much easiler to understand before.
After several investigation, the most comprehensive material for me was the Wikipedia page.
In functional programming, a monad is a type that wraps another type and gives some form of quality to the underlying type.
Here is how I built the knowledge:
 Suppose a type
T
.  A monad consists of three components.
 A type constructor
M
.  A type converter
unit : T > M T
.  A combinator/bind
(>>=) : (M T, T > M U)
 A type constructor
 A monad constructor
M
create a new type from a typeT
, calledM T
. This type is called a monadic type. The value of the typeM T
is called a monadic value.  A type converter wraps the original value of type
T
in a new monadic value of typeM T
. The converter is often called unit or return:unit: T > M T
.  A monad also has a capability of feeding a value of type
T
in a monadic functionf
. A “monadic function” is the function which returns a monad. This is the role of a combinator, and it is calledbind
orflatMap
. What combinator do is unwrapping a monadic variable, then inserting it into a monadic function/expression, and finally resulting in a new monadic value:mx : M T
andf : T → M U
, then(mx >>= f) : M U
.
Combinator

Why the name is “combinator”? Because it combines two monadic functions. $$ \lambda: a \rightarrow f(a) \underbrace{>>=}_{combinator} \lambda: b \rightarrow g(b) $$ , where $f: T \rightarrow M U$ and $g: U \rightarrow MV$.

The actual behavior of combinator is depend on implementation which fullfill associativity and unit, corresponding to its monad.

“How to unwrap the monadic value from $f$” is one of responsibility of combinator.

About combinator: https://www.youtube.com/watch?v=ZhuHCtR3xq8

Monad by example
Maybe
in Haskell (orOption
in Rust): https://www.youtube.com/watch?v=t1e8gqXLbsU
Two sideways (should be reviewed):
 Lambda calculus: https://www.youtube.com/watch?v=eis11j_iGMs
 Recursion (Y combinator): https://www.youtube.com/watch?v=9T8A89jgeTI
 How Y combinator does recursion: https://en.wikipedia.org/wiki/Fixedpoint_combinator#Fixedpoint_combinators_in_lambda_calculus
Old notes
Overview of FP
Good entry point how to think FP:
https://medium.com/@olxc/switchingfromooptofunctionalprogramming4187698d4d3
related topics of the post above
 Type theory: https://en.wikipedia.org/wiki/Type_theory
 Polymorphism (type theory): https://en.wikipedia.org/wiki/Polymorphism_(computer_science)