
This snippet increments integer n times using state monad. How to call: main 10 5 module BasicState where import Control.Monad.State.Strict import Debug.Trace type St a = State a a -- caller is not aware that main uses state -- mai is a pure function main :: Int -> Int -> Int main start0 repeat0 = evalState (repeatN repeat0) start0 -- state-passing computation repeatN :: Int -> St Int repeatN n0 -- repeat n times | n0 < 1 = get -- current state | otherwise = do withState pureStateModifier get -- update state repeatN $ n0 - 1 -- recurse -- state unaware modifier function pureStateModifier :: Int -> Int pureStateModifier = (+ 1)

I don't think that code snippets presented without motivation, explanation,
or commentary are an effective way to teach.
On Sun, Dec 13, 2015 at 9:14 AM Imants Cekusins
This snippet increments integer n times using state monad.
How to call: main 10 5
module BasicState where
import Control.Monad.State.Strict import Debug.Trace
type St a = State a a
-- caller is not aware that main uses state -- mai is a pure function main :: Int -> Int -> Int main start0 repeat0 = evalState (repeatN repeat0) start0
-- state-passing computation repeatN :: Int -> St Int repeatN n0 -- repeat n times | n0 < 1 = get -- current state | otherwise = do withState pureStateModifier get -- update state repeatN $ n0 - 1 -- recurse
-- state unaware modifier function pureStateModifier :: Int -> Int pureStateModifier = (+ 1) _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I don't think that code snippets presented without motivation, explanation, or commentary are an effective way to teach.
I agree, they are not. However it took me a better part of today to write this snippet. If I saw it as it is, I could proceed with my coding task without delay. I posted it just in case someone faces a similar problem and could benefit from it. This is to complement other available materials, not to replace them.

Perhaps you would be so kind as to provide the `motivation',
`explanation' and maybe some `commentary' for good measure. *smile*
On 13/12/2015, Imants Cekusins
I don't think that code snippets presented without motivation, explanation, or commentary are an effective way to teach.
I agree, they are not.
However it took me a better part of today to write this snippet.
If I saw it as it is, I could proceed with my coding task without delay.
I posted it just in case someone faces a similar problem and could benefit from it.
This is to complement other available materials, not to replace them.

Is your intent is to call main function? How do you compile this module, is
it part of a bigger project, or just a single-file program? If single-file
-- rename main to something else, then create a main function which has a
type "main :: IO ()", and put something like:
main :: IO ()
main = print (mainRoutine 10 5)
It'll call mainRoutine with 10 and 5 args and print its result. You can
then run it with "runhaskell MyProg.hs"
On Sun, Dec 13, 2015 at 7:14 PM, Imants Cekusins
This snippet increments integer n times using state monad.
How to call: main 10 5
module BasicState where
import Control.Monad.State.Strict import Debug.Trace
type St a = State a a
-- caller is not aware that main uses state -- mai is a pure function main :: Int -> Int -> Int main start0 repeat0 = evalState (repeatN repeat0) start0
-- state-passing computation repeatN :: Int -> St Int repeatN n0 -- repeat n times | n0 < 1 = get -- current state | otherwise = do withState pureStateModifier get -- update state repeatN $ n0 - 1 -- recurse
-- state unaware modifier function pureStateModifier :: Int -> Int pureStateModifier = (+ 1) _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I see. Sorry, for some reason it wasn't clear for me :)
On Tue, Dec 15, 2015 at 5:05 PM, Imants Cekusins
Is your intent is to call main function? ...
Hello Kostiantyn,
my intent was to share a working snippet with those who may run into a similar problem.
I can call it and run it ok. I understand that you could run it without problems too if you wanted. If not, kindly let me know. _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (4)
-
Imants Cekusins
-
Kostiantyn Rybnikov
-
MJ Williams
-
Rein Henrichs