Computing a fibonacci number with State Monad
In this recipe, we will use the State Monad as defined by the mtl library. The mtl library defines many useful, well-articulated monads. We will use the State Monad to calculate a fibonacci number.
How to do it...
- Create a new project using the
simpleStack template:
stack new fibonacci-state simple
- Open
fibonacci-state.cabaland add packagesmtlandcontainersto thebuild-dependssubsection of theexecutablesection:
executable fibonacci-state
hs-source-dirs: src
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, mtl
, containers- Build the project so that the dependent packages will be pulled by
stack:
stack build
- Open
src/Main.hs. We will be editing this file. - Import the following modules after initial module declaration:
import Control.Applicative
import Control.Monad.State
import Data...