title: Getting the head and tail of a custom list type in Haskell
body: <p>I have a custom list type:</p>
  
  <pre><code>data NNList a = Sing a | Append ( NNList a) ( NNList a) deriving (Eq)
  data CList a = Nil | NotNil ( NNList a) deriving (Eq)
  </code></pre>
  
  <p>I'm trying to implement a function that returns the head and tail of a list:</p>
  
  <blockquote>
    <p>cListGet :: CList a -> Maybe (a, CList a)</p>
  </blockquote>
  
  <p>My attempt:</p>
  
  <pre><code>cListGet :: CList a -&gt; Maybe (a, CList a)
  cListGet Nil             = Nothing
  cListGet xs@(NotNil nxs) =
  case nxs of
    Sing x        -&gt; (x, Nil)
    Append l r    -&gt; ((fst $ cListGet (NotNil l)), (Append (snd $ cListGet (NotNil l)), r))
  </code></pre>
  
  <p>Which to me means keep going leftwards until I get a single. Once I get the single element (head), return the element and a Nil list. This Nil list is then combined with the list before it's returned as the final result.</p>
  
  <p>I'm not even sure if the logic is 100% correct.</p>