Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>return</code> in haskell does not do the same thing as <code>return</code> in other languages. Instead, what <code>return</code> does is to inject a value into a monad (in this case <code>IO</code>). You have a couple of options</p> <p>the most simple is to use if</p> <pre><code>scrapePage :: String -&gt; IO () scrapePage url = do doc &lt;- fromUrl url title &lt;- liftM headMay $ runX $ doc &gt;&gt;&gt; css "head.title" &gt;&gt;&gt; getText if (isNothing title) then return () else do date &lt;- liftM headMay $ runX $ doc &gt;&gt;&gt; css "span.dateTime" ! "data-utc" if (isNothing date) then return () else do -- etc -- make page object and send it to db return () </code></pre> <p>another option is to use <code>unless</code></p> <pre><code>scrapePage url = do doc &lt;- fromUrl url title &lt;- liftM headMay $ runX $ doc &gt;&gt;&gt; css "head.title" &gt;&gt;&gt; getText unless (isNothing title) do date &lt;- liftM headMay $ runX $ doc &gt;&gt;&gt; css "span.dateTime" ! "data-utc" unless (isNothing date) do -- etc -- make page object and send it to db return () </code></pre> <p>the general problem here is that the <code>IO</code> monad doesn't have control effects (except for exceptions). On the other hand, you could use the maybe monad transformer</p> <pre><code>scrapePage url = liftM (maybe () id) . runMaybeT $ do doc &lt;- liftIO $ fromUrl url title &lt;- liftIO $ liftM headMay $ runX $ doc &gt;&gt;&gt; css "head.title" &gt;&gt;&gt; getText guard (isJust title) date &lt;- liftIO $ liftM headMay $ runX $ doc &gt;&gt;&gt; css "span.dateTime" ! "data-utc" guard (isJust date) -- etc -- make page object and send it to db return () </code></pre> <p>if you really want to get full blown control effects you need to use <code>ContT</code></p> <pre><code>scrapePage :: String -&gt; IO () scrapePage url = runContT return $ do doc &lt;- fromUrl url title &lt;- liftM headMay $ runX $ doc &gt;&gt;&gt; css "head.title" &gt;&gt;&gt; getText when (isNothing title) $ callCC ($ ()) date &lt;- liftM headMay $ runX $ doc &gt;&gt;&gt; css "span.dateTime" ! "data-utc" when (isNothing date) $ callCC ($ ()) -- etc -- make page object and send it to db return () </code></pre> <p>WARNING: none of the above code has been tested, or even type checked!</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.
    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