Back to Fpinscala

11.Answer

answerkey/monads/11.answer.md

latest663 B
Original Source

For Option, we again consider both cases None and Some and expand the equation. The monadic unit is the Some(_) constructor.

Left identity is trivially true for None:

scala
None.flatMap(Some(_)) == None

And here it is for Some:

scala
Some(v).flatMap(Some(_)) == Some(v)

Substitute the definition of flatMap:

scala
Some(v) == Some(v)

Right identity is just as easy for None:

scala
Some(None).flatMap(f) == f(None)

Substitute definition of flatMap:

scala
f(None) == f(None)

And for Some:

scala
Some(Some(v)).flatMap(f) == f(Some(v))

Substitute definition of flatMap:

scala
f(Some(v)) == f(Some(v))