title: How to add two lists like matlab in Haskell?
body: <p>I was wondering if it is possible to write a Haskell add operator that can work on two numbers, two lists of numbers, two matrices of numbers, ... and so on. That is, is it possible to define an <code>&lt;+&gt;</code> such that all of the following works?:</p>
  
  <pre><code>1 &lt;+&gt; 2 = 3
  [1] &lt;+&gt; [2] = [3]
  [[1]] &lt;+&gt; [[2]] = [[3]]
  ...
  </code></pre>
  
  <p>I know that as in <a href="https://stackoverflow.com/questions/4776750/what-is-an-idiomatic-way-to-add-lists-in-haskell">What is an idiomatic way to add lists in Haskell?</a> , we can do <code>+</code> for <code>a</code>, <code>zipWith (+)</code> for <code>[a]</code>. Probably <code>zipWith (zipWith (+))</code> for <code>[[a]]</code>, along the same line, and so on...</p>
  
  <p>But can it be done using one operator (as Matlab was able) using type classes or other features of Haskell?</p>
  
  <p>I know this is syntatic sugar, but it would be sweet if possible. </p>
  
  <p>-- update --</p>
  
  <p>I see that maybe it's problematic to use <code>(Num a)</code> as explained in @DanielWagner's answer, and it's probably the best to define it individually for <code>Integer, Double</code> etc. for base cases.</p>
  
  <p>Just for the record, I just tried as suggested:</p>
  
  <pre><code>{-# LANGUAGE FlexibleInstances, UndecidableInstances #-}
  class Additive a where (&lt;+&gt;) :: a -&gt; a -&gt; a
  instance Num a =&gt; Additive a where (&lt;+&gt;) = (+)
  instance Additive a =&gt; Additive [a] where (&lt;+&gt;) = zipWith (&lt;+&gt;)
  </code></pre>
  
  <p>Or </p>
  
  <pre><code>{-# LANGUAGE DefaultSignatures #-}
  class Additive a where
      (&lt;+&gt;) :: a -&gt; a -&gt; a
      default (&lt;+&gt;) :: Num a =&gt; a -&gt; a -&gt; a
      (&lt;+&gt;) = (+)
  </code></pre>
  
  <p>In either case, there is an error when loading the <code>.hs</code> file or when evaluating <code>[[1,2]] &lt;+&gt; [[3,4]]</code>.</p>