Note that there are some explanatory texts on larger screens.

plurals
  1. POiterate list creation from IO Int, How to?
    text
    copied!<p>I am playing with <a href="http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=12345" rel="nofollow noreferrer">linkedlist</a> problem in python challenge that require querying a next value (guess it be Int).</p> <p>I create function for get the next value as follows</p> <pre><code>url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=" getNext :: Int -&gt; IO Int getNext x = do rsp &lt;- simpleHTTP (getRequest $ url ++ show x) bdy &lt;- getResponseBody rsp let num = last $ splitWhen (==' ') bdy return (read num::Int) </code></pre> <p>and it work fine (in ghci)</p> <pre><code>&gt; getNext 12345 44827 &gt; getNext 44827 45439 </code></pre> <p>While I suppose to <em>repeatedly call</em> getNext until I found the answer, I think I should keep the history like I can do in non-monadic world so I can continue from the last value in case something fail.</p> <pre><code>&gt; let nX x = x + 3 &gt; :t nX nX :: Num a =&gt; a -&gt; a &gt; take 10 $ iterate nX 1 [1,4,7,10,13,16,19,22,25,28] </code></pre> <p>I think it should be a monadic lifted version of iterate and found <code>iterateM_</code> from Control.Monad.Loops but it didn't work as I expected. There is nothing shown (I think _ suffix mean discard the result but there is no <em>iterateM</em>)</p> <pre><code>&gt; :t iterate iterate :: (a -&gt; a) -&gt; a -&gt; [a] &gt; :t iterateM_ iterateM_ :: Monad m =&gt; (a -&gt; m a) -&gt; a -&gt; m b </code></pre> <p><strong>Question</strong> is how can I get [Int] as in non-monadic iteration. I think I want a function that return <code>IO [Int]</code> to be able to pull-out and filter/process in my code like this</p> <pre><code>main = do i &lt;- getAllList let answer = last i -- or could be a repeated converged value, don't know yet putStrLn (show answer) getAllList :: IO [Int] </code></pre>
 

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