Note that there are some explanatory texts on larger screens.

plurals
  1. PO'Multiple declarations of' error when pattern matching with wildcard
    primarykey
    data
    text
    <p>For learning purposes I am trying to write my own implementation of the <code>zipWith</code> function. However, I am hitting an issue with pattern matching on edge cases with <code>_</code>. First I will describe the good case, then the bad case. Hopefully someone will be able to explain why they behave differently. Thanks</p> <p>If I write the <code>zipWith</code> function as follows, it works (Note the order of the edge cases matching empty list on lines 2 &amp; 3):-</p> <pre><code>zipWith' :: (a -&gt; b -&gt; c) -&gt; [a] -&gt; [b] -&gt; [c] zipwith' _ [] _ = [] zipWith' _ _ [] = [] zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys </code></pre> <p>Compiling in GHCI:-</p> <pre><code>ghci&gt; :l ZipWith.hs [1 of 1] Compiling Main ( ZipWith.hs, interpreted ) </code></pre> <p>Ok, the above is fine, yet if I swap the pattern matching for the edge cases around GHCI throws 'Multiple declarations of' error for lines 2 and 4.</p> <pre><code>zipWith' :: (a -&gt; b -&gt; c) -&gt; [a] -&gt; [b] -&gt; [c] zipWith' _ _ [] = [] zipwith' _ [] _ = [] zipWith' f (x:xs) (y:ys) = f x y : zipWith' f xs ys </code></pre> <p>Compiling in GHCI:-</p> <pre><code>ZipWith.hs:4:0: Multiple declarations of `Main.zipWith'' Declared at: ZipWith.hs:2:0 ZipWith.hs:4:0 Failed, modules loaded: none. </code></pre> <p>I'm stumped...</p> <ol> <li>Looking at the patterns on lines 2 and 4 they seem mutually exclusive but i'm obviously missing something fundamental here</li> <li>Why would switching the patterns on lines 2 and 3 cause the compilation error to go away.</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload