Desugared Example Code module Main where import Control.Monad.State.Strict (StateT(..)) import Data.Functor.Identity (Identity(..)) type OccurrenceValue = Int type OccurrenceState = Int countOccurrence :: String -> StateT OccurenceState Identity OccurenceValue countOccurrence [] = StateT $ \s -> Identity (s, s) countOccurrence (x:xs) = StateT $ \s -> runStateT (k x xs s) s m1 :: Char -> […]

# Category: Uncategorized

## Desugaring the State (Transformer) Monad Part III

In this part we will expand the example code. Expanding countOccurrence (x:xs) countOccurrence (x:xs) = do counter <- get if ‘s’ == x then put $ counter + 1 else put counter countOccurrence xs — rewrite of if and else to a case expr as core does also countOccurrence (x:xs) = do counter <- get […]

## Desugaring the State (Transformer) Monad Part II

Example This is the example code. We try to count all the occurences of s inside a string. If a char is a ‘s’ then we increment the counter by 1. At the end we print the final value of the counter. module Main where import Control.Monad.State.Strict type OccurrenceState = Int type OccurrenceValue = Int […]

## Desugaring the State (Transformer) Monad Part I

Why is learning Monads difficult? One of the things Haskell is most famous for are monads. There is a page on the Haskell wiki https://wiki.haskell.org/Monad_tutorials_timeline#year_2015 where a lot of Monad tutorials a listed. So it seems that understanding monads is somehow difficult. Why is this the case? There is this great ebook of Stephen Diehl […]

## Recent Comments