title: How to solve this Haskell type error? Expected and actual types do not match
body: <p>I'm trying to extract all the largest integers from a list to another list using foldr and a helper function. My understanding of foldr is in conflict with the error messages. Here is my code:</p>
  <pre><code>largest :: [Int] -&gt; [Int]
  largest [] = []
  largest xs = foldr largestHelper [] xs
  
  largestHelper :: [Int] -&gt; Int -&gt; [Int]
  largestHelper [] prev = [prev]
  largestHelper (lrg : lrgs) prev | lrg &lt; prev  = [prev]
                                  | lrg == prev = prev : (lrg : lrgs)  
                                  | otherwise   = (lrg : lrgs)
  </code></pre>
  <p>To me it looks like the first function to be evaluated is <code>largestHelper</code> with the last element of the integer list and <code>[]</code>. Next is <code>largestHelper</code> using the second last element of the list and the previous iteration which is simply the last element because it is the largest this far. And so on. So in the end <code>largest xs</code> has evaluated to a list which contains one or more integers with the largest value found from the given list.</p>
  <p>Here are the errors:</p>
  <pre><code>largest.hs:3:20: error:
      * Couldn't match type `[Int]' with `Int'
        Expected type: [Int] -&gt; [Int] -&gt; [Int]
          Actual type: [Int] -&gt; Int -&gt; [Int]
      * In the first argument of `foldr', namely `largestHelper'
        In the expression: foldr largestHelper [] xs
        In an equation for `largest':
            largest xs = foldr largestHelper [] xs
    |
  3 | largest xs = foldr largestHelper [] xs
    |                    ^^^^^^^^^^^^^
  
  largest.hs:3:37: error:
      * Couldn't match type `Int' with `[Int]'
        Expected type: [[Int]]
          Actual type: [Int]
      * In the third argument of `foldr', namely `xs'
        In the expression: foldr largestHelper [] xs
        In an equation for `largest':
            largest xs = foldr largestHelper [] xs
    |
  3 | largest xs = foldr largestHelper [] xs
    |
  </code></pre>
  <p>The first one seems to say that <code>largestHelper</code> is actually receiving two lists of integers instead of the one it is constructing. But doesn't <code>foldr</code> give elements of the list given to <code>largest</code> one by one and thus the second argument for <code>largestHelper</code> would indeed be an integer instead of a list of integers?
  The second one seems to say that the argument for <code>largest</code> is actually <code>[[Int]]</code> instead of <code>[Int]</code>. I don't understand that.</p>
  <p>I would appreciate very much clarification to this confusion. I don't know yet if this works, because it doesn't compile and as a total beginner in functional programming and Haskell, I would appreciate also comments considering the logic and style of the code.</p>