title: Haskell Insert function for Binary Trees
body: <p>I'm trying to create a function called "insertm" that is supposed to insert a key and value into a binary tree.If the key already exists it should return "nothing". If not it should insert the key and value into the tree based on it's value. I was able to get most of it to go, but I'm getting an error that i'm not sure how to fix. </p>
  
  <p>Here is an example:</p>
  
  <pre><code>      TestQ4&gt; insertm 25 "vw" t5
        Just (10:"ghi")&lt;$,(30:"def")&lt;(20:"abc")&lt;$,(25:"vw")&gt;,$&gt;&gt;
        TestQ4&gt; insertm 20 "vw" t5
        Nothing
  </code></pre>
  
  <p>Here is my code: </p>
  
  <pre><code>     data BinaryTree a b = Leaf | Node a b (BinaryTree a b) (BinaryTree a b) 
  
       insertm :: (Ord a, Show a, Show b) =&gt;
       a -&gt; b -&gt; BinaryTree a b -&gt; Maybe (BinaryTree a b)
  
       insertm val key Leaf = Just (Node val key Leaf Leaf)
       insertm x y (Node val key left right)
            | x == val = Nothing
            | x &lt; val = Just (Node val key (insertm x y left) right)
            | otherwise = Just (Node val key left (insertm x y right))
  </code></pre>
  
  <p>And this is the error i get:</p>
  
  <pre><code>       * Couldn't match expected type `BinaryTree a b'
                with actual type `Maybe (BinaryTree a b)'
         * In the fourth argument of `Node', namely `(insertm x y right)'
           In the first argument of `Just', namely
             `(Node val key left (insertm x y right))'
           In the expression: Just (Node val key left (insertm x y right))
         * Relevant bindings include
            right :: BinaryTree a b (bound at TestQ4.hs:101:32)
            left :: BinaryTree a b (bound at TestQ4.hs:101:27)
            key :: b (bound at TestQ4.hs:101:23)
            val :: a (bound at TestQ4.hs:101:19)
            y :: b (bound at TestQ4.hs:101:11)
            x :: a (bound at TestQ4.hs:101:9)
           (Some bindings suppressed; use -fmax-relevant-binds=N or -fno-max- 
              relevant-binds)
  
            | x &lt; val = Just (Node val key (insertm x y left) right)
                                            ^^^^^^^^^^^^^^^^
  </code></pre>
  
  <p>I also get the error for my otherwise case. So im a bit stuck any help would be appreciated.</p>