Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In Haskell there exists a concept called pure code. Pure code does not have any of the following: user-input values, system calls, pseudorandom number generation, calls to non-pure code, etc.</p> <p>Pure functions are guaranteed to always always always have the same behavior based on the lexical content of your program (e.g. they can return different values, but the reason they return different values cannot depend on "the world").</p> <p><strong>This is a very powerful policy in functional languages, and allows very powerful code. For example, you know that calling a function won't change some unrelated global variable or the state of some other data structure.</strong> I often apply this policy to python code.</p> <p>The way Haskell enforces the purity-impurity thing is with the IO monad.</p> <p>Any of things that "the world" touches are wrapped up in the IO monad, which represented that the values have been "tainted". If anything touches these "tainted" values, the values they return must also be tainted.</p> <p>You will need to run your pure code within the IO monad if you want it to have access to those values. Once within the IO monad, you can "unwrap" the value you've read and pass it into your pure code, then your pure code returns value, then you can print your value. Everything works as expected, but you have to do it in the IO monad.</p> <p>Note that it is good form to ensure that most of your code is written in a pure, functional form outide of the IO monad. e.g. a pure <code>doStuffWithRectangles</code> function, that is then called from within the IO monad.</p> <p>The beautiful thing about it is that your pure code doesn't need to know the <code>Rectangle</code> value has been tainted by being of type <code>IO Rectangle</code>. As long as you work in the IO monad, your pure code will think it is just a normal <code>Rectangle</code>.</p> <hr> <p>And just to make this answer more explicit: <code>readFile</code> returns things wrapped in the IO monad, because those things come from "the world" (the filesystem) and for example if you changed the file during program execution, it might return a different value.</p> <pre><code>--db = loadFile "db.txt" REMOVED-- main = do --within the IO monad putStrLn "Choose option:" n &lt;- getLine **DB &lt;- LOADFILE "db.txt"** --db is now a pure value case n of "1" -&gt; do menuRectangles db; main "2" -&gt; putStrLn "bye, bye" otherwise -&gt; do putStrLn "Bad option"; main </code></pre> <p>More information: <a href="http://www.haskell.org/tutorial/io.html" rel="nofollow">http://www.haskell.org/tutorial/io.html</a></p> <p>A good online/offile read: <a href="http://learnyouahaskell.com/" rel="nofollow">http://learnyouahaskell.com/</a></p> <p>Also a good online/offile read: <a href="http://book.realworldhaskell.org/" rel="nofollow">http://book.realworldhaskell.org/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload