<div dir="ltr"><div><div><div><div><div><div><div><div>Hi,<br></div>The problem is in your "runProgram" and "runInstructionOrStop" functions: their "state" parameter should have type "ProgramState" and not "IO ProgramState" and they have to be fixed accordingly.<br><br>With your code, you "append new actions" (opReadString "a", etc.) to "state" with "(state >>= (getInstruction pos program))" and you execute the whole program up to the current instruction each time in "(fmap (checkProgramBounds program) state)" and "(fmap position state)".</div><div><br></div>In addition, this is what I would do to enhance your code (in order):<br></div>1) Use do-notation in runProgram and runInstructionOrStop (i.e. remove "perform" let-bindings and ">>=") to make the code easier to understand. <br></div>2) Don't store the program in a list: length and (!!) are O(n) with lists. Use Vector instead.<br></div>3) Put the program in ProgramState<br></div>4) More advanced (State monad + monad transformers): use a Program type defined as: type Program a = StateT ProgramState IO a<br></div>5) Rewrite runProgram and runInstructionOrStop with "sequence" from Control.Monad<br><br></div><div>Regards,<br></div>Sylvain<br><div><div><div><div><div><div><div><div><div><div><br></div></div></div></div></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2016-02-17 21:13 GMT+01:00 Dmitry Mamontov <span dir="ltr"><<a href="mailto:mamontov.dp@gmail.com" target="_blank">mamontov.dp@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">I am a Haskell beginner. I've tried to write a tiny VM-like thing but ran into a problems with IO and evaluation order.<div><br></div><div>Here is the full source code of this VM: <a href="https://gist.github.com/mamontov-cpp/c4d8e2e46e7541c0c646" target="_blank">https://gist.github.com/mamontov-cpp/c4d8e2e46e7541c0c646</a> . This code is supposed to read 4 strings and output them in reverse order.</div><div><br></div><div>While it seems to read some strings, it requests much more than one string from user on each step of evaluation of meta-program, stored as array in main function (which is just wrong).</div><div><br></div><div>I assume the problem with this program, is that the main state, being immutable, seems to get re-evaluated on several steps inside of internal parts of runProgram and runInstructionOrStop, forcing it to repeat requesting data from user.</div><div><br></div><div>So, how can I prevent this main state from being re-evaluated in following code ? Or is there any other problems, which I don't see?</div><div><br></div><div><br></div><div><br></div></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>