title: Haskell - Extract Maybe in Monad
body: <p>I am working on problem 9 of Project Euler, and I have a question about the best way to extract a maybe value inside another monad. The problem asks to find 'a', 'b', 'c' that satisfy:</p>
  
  <ul>
  <li>a^2 + b^2 = c^2</li>
  <li>a + b + c = 1000</li>
  </ul>
  
  <p>I have written the following code that solves the problem:</p>
  
  <pre><code>problem9 :: (Integral a) =&gt; a -&gt; [(a, a, a)]
  problem9 n =
      do
          a &lt;- [1..n]
          b &lt;- [1..a]
          c &lt;- fromJustM (findC a b)
          guard (a + b + c == n)
          return (a, b, c)
  </code></pre>
  
  <p>'c' can be computed analytically, but, since it may not exist, I return a maybe value.</p>
  
  <pre><code>findC :: (Integral a) =&gt; a -&gt; a -&gt; Maybe a
  findC a b = ... (implementation) ...
  </code></pre>
  
  <p>To extract the maybe value inside the list monad, I have created the following function: </p>
  
  <pre><code>fromJustM :: (Monad m) =&gt; Maybe a -&gt; m a
  fromJustM (Just a) = return a
  fromJustM Nothing = fail ""
  </code></pre>
  
  <p>It seems like this should be a common operation, so is there a standard library function that does this, or is there a more idiomatic way to do it?</p>