Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should first turn the <code>[Maybe a]</code> into a <code>Maybe [a]</code> with all the <code>Just</code> elements (yielding <code>Nothing</code> if any of them are <code>Nothing</code>). This can be done using <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:sequence" rel="noreferrer">sequence</a>, using Maybe's Monad instance:</p> <pre><code>GHCi&gt; sequence [Just 1, Just 2] Just [1,2] GHCi&gt; sequence [Just 1, Just 2, Nothing] Nothing </code></pre> <p>The <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Control-Monad.html#sequence" rel="noreferrer">definition of sequence</a> is equivalent to the following:</p> <pre><code>sequence [] = return [] sequence (m:ms) = do x &lt;- m xs &lt;- sequence ms return (x:xs) </code></pre> <p>So we can expand the latter example as:</p> <pre><code>do x &lt;- Just 1 xs &lt;- do y &lt;- Just 2 ys &lt;- do z &lt;- Nothing zs &lt;- return [] return (z:zs) return (y:ys) return (x:xs) </code></pre> <p>Using the <a href="http://www.haskell.org/haskellwiki/Monad_laws" rel="noreferrer">do-notation expression of the monad laws</a>, we can rewrite this as follows:</p> <pre><code>do x &lt;- Just 1 y &lt;- Just 2 z &lt;- Nothing return [x, y, z] </code></pre> <p>If you know how the Maybe monad works, you should now understand how <code>sequence</code> works to achieve the desired behaviour. :)</p> <p>You can then compose this with <code>foldr</code> using <code>(&lt;$&gt;)</code> (from <a href="http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Applicative.html#v:-60--36--62-" rel="noreferrer">Control.Applicative</a>; equivalently, <code>fmap</code> or <code>liftM</code>) to fold your binary function over the list:</p> <pre><code>GHCi&gt; foldl' (+) 0 &lt;$&gt; sequence [Just 1, Just 2] Just 3 </code></pre> <p>Of course, you can use any fold you want, such as <code>foldr</code>, <code>foldl1</code> etc.</p> <p>As an extra, if you want the result to be <code>Nothing</code> when the list is empty, and thus be able to omit the zero value of the fold without worrying about errors on empty lists, then you can use this fold function:</p> <pre><code>mfoldl1' :: (MonadPlus m) =&gt; (a -&gt; a -&gt; a) -&gt; [a] -&gt; m a mfoldl1' _ [] = mzero mfoldl1' f (x:xs) = return $ foldl' f x xs </code></pre> <p>and similarly for <code>foldr</code>, <code>foldl</code>, etc. You'll need to import <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html" rel="noreferrer">Control.Monad</a> for this.</p> <p>However, this has to be used slightly differently:</p> <pre><code>GHCi&gt; mfoldl1' (+) =&lt;&lt; sequence [Just 1, Just 2] Just 3 </code></pre> <p>or</p> <pre><code>GHCi&gt; sequence [Just 1, Just 2] &gt;&gt;= mfoldl1' (+) Just 3 </code></pre> <p>This is because, unlike the other folds, the result type looks like <code>m a</code> instead of <code>a</code>; it's a <em>bind</em> rather than a <em>map</em>.</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