title: Haskell - Maybe and Recursion
body: <p>I want to take a string and turn into into a list of <code>Direction</code>. For example, <code>"UDDUD"</code> should return <code>[U,D,D,U,D]</code>, whereas any string that does not contain a <code>U</code> or <code>D</code> returns <code>Nothing</code> (e.g. "UDYD" returns <code>Nothing</code>).</p>
  
  <pre><code>data Direction = U | D
      deriving (Show, Eq)
  -- where U is Up and D is Down
  
  findDirection :: [Char] -&gt; Maybe [Direction]
  findDirection [] = Nothing
  findDirection ['U'] = Just [U]
  findDirection ['D'] = Just [D]
  findDirection (x:xs)
      | x == 'U' = Just (U : findDirection xs)
      | x == 'D' = Just (D : findDirection xs)
      | otherwise = Nothing
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Couldn't match expected type ‘[Direction]’
                  with actual type ‘Maybe [Direction]’
      In the second argument of ‘(:)’, namely ‘findDirection xs’
      In the first argument of ‘Just’, namely
        ‘(U : findDirection xs)’
  
  Test_findDirection.hs:8:32:
      Couldn't match expected type ‘[Direction]’
                  with actual type ‘Maybe [Direction]’
      In the second argument of ‘(:)’, namely ‘findDirection xs’
      In the first argument of ‘Just’, namely
        ‘(D : findDirection xs)’
  </code></pre>
  
  <p>As I understand it, <code>Just (D : findDirection xs)</code> and <code>Just (U : findDirection xs)</code> are of type <code>[Direction]</code>? Why is this the case? What am I doing wrong, here?</p>