Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is that the function <code>encode</code> expects "unpacked" lists, but you pass a "packed" list.</p> <p>Adding type signatures helps a lot here, I added a type signature for <code>encode'</code></p> <pre><code>{-# LANGUAGE ScopedTypeVariables #-} pack :: (Eq a) =&gt; [a] -&gt; [[a]] pack [] = [] pack (x:xs) = (x : takeWhile (== x) xs) : (pack $ dropWhile (== x) xs) encode :: forall a. (Eq a) =&gt; [a] -&gt; [(Int, a)] encode [] = [] encode list = (encode' $ head packed) : (encode $ tail packed) where packed = pack list encode' :: [a] -&gt; (Int, a) encode' l = (length l, head l) </code></pre> <p>and the compiler finds the error quickly:</p> <pre><code>[1 of 1] Compiling Main ( test.hs, interpreted ) test.hs:9:42: Couldn't match type `a' with `[a]' `a' is a rigid type variable bound by the type signature for encode :: Eq a =&gt; [a] -&gt; [(Int, a)] at test.hs:8:1 Expected type: [(Int, a)] Actual type: [(Int, [a])] In the second argument of `(:)', namely `(encode $ tail packed)' In the expression: (encode' $ head packed) : (encode $ tail packed) Failed, modules loaded: none. </code></pre> <p>Because that would only work if <code>a</code> was the same as <code>[a]</code> and therefore the same as <code>[[a]]</code> etc. That's an infinite type error. Or simply the difference between a "packed" list (<code>[[a]]</code>) and a "unpacked" list (<code>[a]</code>) in your sample.</p> <p>(For better understanding: "packed" list is a list after applying the <code>pack</code> function ;-) )</p> <p><em>edit: fixed ScopedTypeVariables vs. ExistentialQuantification error, thanks John L</em></p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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