Based on the question How can I re-assign a variable in a function in Haskell?, there is a haskell solution to change the total congo elephant count Congo 0
in a function
:
main' :: StateT Congo IO ()
main' =
do
printElephant
function 2
printElephant
-- run program:
main :: IO ()
main = Congo 0 & runStateT main' & void
-- outputs:
0
2
After reading the Computation Expression series, I still don't know how to write a CE builder for this problem correctly. How can I re-assign a variable in a function with F#'s CE?
F# has a first-class support for imperative programming constructs. You just need to mark your let bindings as mutable
. There is no need for computation expressions in this case:
let mutable elephant = 0
printfn "Elephant = %d" elephant
elephant <- 2
printfn "Elephant = %d" elephant