Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Problems with IO an laziness (Dmitry Mamontov)
   2. Re:  Problems with IO an laziness (Sylvain Henry)
   3. Re:  Problems with IO an laziness (Dmitry Mamontov)


----------------------------------------------------------------------

Message: 1
Date: Wed, 17 Feb 2016 23:13:15 +0300
From: Dmitry Mamontov <mamontov...@gmail.com>
To: beginners@haskell.org
Subject: [Haskell-beginners] Problems with IO an laziness
Message-ID:
        <campq2dx28j9mxxdyuzwzsvxujsetmaz_pjufvfafp8ytrvn...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

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.

Here is the full source code of this VM:
https://gist.github.com/mamontov-cpp/c4d8e2e46e7541c0c646 . This code is
supposed to read 4 strings and output them in reverse order.

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).

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.

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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160217/8e1ab32f/attachment-0001.html>

------------------------------

Message: 2
Date: Thu, 18 Feb 2016 00:54:35 +0100
From: Sylvain Henry <hsy...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Problems with IO an laziness
Message-ID:
        <CAPmptcXL3s+J+MwHLqEuRMNc6GyhpQLXyHpz3WoSx=3=-so...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi,
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.

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)".

In addition, this is what I would do to enhance your code (in order):
1) Use do-notation in runProgram and runInstructionOrStop (i.e. remove
"perform" let-bindings and ">>=") to make the code easier to understand.
2) Don't store the program in a list: length and (!!) are O(n) with lists.
Use Vector instead.
3) Put the program in ProgramState
4) More advanced (State monad + monad transformers): use a Program type
defined as: type Program a = StateT ProgramState IO a
5) Rewrite runProgram and runInstructionOrStop with "sequence" from
Control.Monad

Regards,
Sylvain


2016-02-17 21:13 GMT+01:00 Dmitry Mamontov <mamontov...@gmail.com>:

> 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.
>
> Here is the full source code of this VM:
> https://gist.github.com/mamontov-cpp/c4d8e2e46e7541c0c646 . This code is
> supposed to read 4 strings and output them in reverse order.
>
> 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).
>
> 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.
>
> 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?
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160218/6d43b083/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 18 Feb 2016 09:06:18 +0300
From: Dmitry Mamontov <mamontov...@gmail.com>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Problems with IO an laziness
Message-ID:
        <campq2dxdgbu+97g8pqxcubzod2bxfxixcm4qe+xagivpqev...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Thanks for your help. This solved my problems.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20160218/ab2656f7/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 92, Issue 20
*****************************************

Reply via email to