From 29b9359b1dfbfd3ff7a78456f14f1ed9d9967ca6 Mon Sep 17 00:00:00 2001 From: WJH Date: Sat, 18 May 2019 20:28:48 +0800 Subject: [PATCH] Explain purpose of Kleisli Categories from the programmer's perspective --- src/content/1.4/kleisli-categories.tex | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/content/1.4/kleisli-categories.tex b/src/content/1.4/kleisli-categories.tex index ed539f31..ceba1ece 100644 --- a/src/content/1.4/kleisli-categories.tex +++ b/src/content/1.4/kleisli-categories.tex @@ -400,6 +400,23 @@ \section{Kleisli Categories} programs that in imperative languages are traditionally implemented using side effects. +But, what is the real purpose of this concept from the perspective of a +programmer? Improved code maintainability. Recall that the \code{process} +function can actually be written without using the fish operator: + +\begin{snip}{hs} +process :: String -> Writer [String] +process s = + let (s' , log) = upCase s in + let (s'', log') = toWords s' in + (s'', log ++ log') +\end{snip} + +However, this piece of code is not only harder to comprehend, it is also +difficult to modify (imagine that you want to add new operations or +change the sequence of exisitng operations). That is why we need to leverage +the power of Kleisli Categories along with the fish operator. + \section{Challenge} A function that is not defined for all possible values of its argument @@ -443,4 +460,4 @@ \section{Challenge} Compose the functions \code{safe\_root} and \code{safe\_reciprocal} to implement \code{safe\_root\_reciprocal} that calculates \code{sqrt(1/x)} whenever possible. -\end{enumerate} \ No newline at end of file +\end{enumerate}