0

This is the original question:

Modify the result of problem 10 in such a way that if an element has no duplicates it is simply copied into the result list. Only elements with duplicates are transferred as (N E) lists.

Example:

* (encode-modified '(a a a a b c c a a d e e e e))
((4 A) B (2 C) (2 A) D (4 E))

Example in Haskell:

ghci> encodeModified "aaaabccaadeeee"
[Multiple 4 'a',Single 'b',Multiple 2 'c',
Multiple 2 'a',Single 'd',Multiple 4 'e']

This is my solution:

import Data.List

data Encode a = Single a | Multiple Int a deriving (Show)

encodeModified :: (Eq a) => [a] -> [Encode a]
encodeModified xs = [y | x <- group xs, let y = if (length x) == 1   then Single (head x) else Multiple (length x) (head x)]

encodeModified' = map (\xs -> if (length xs == 1) then Single (head xs) else Multiple (length xs) (head xs)) . group

My GHCI is 7.10.3, when I execute :l xx.hs, it gives me:

No instance for (Eq a0) arising from a use of ‘group’
    The type variable ‘a0’ is ambiguous
    Relevant bindings include
      encodeModified' :: [a0] -> [Encode a0] (bound at 11.hs:8:1)
    Note: there are several potential instances:
      instance (Eq a, Eq b) => Eq (Either a b)
        -- Defined in ‘Data.Either’
      instance forall (k :: BOX) (s :: k). Eq (Data.Proxy.Proxy s)
        -- Defined in ‘Data.Proxy’
      instance (GHC.Arr.Ix i, Eq e) => Eq (GHC.Arr.Array i e)
        -- Defined in ‘GHC.Arr’
      ...plus 27 others
    In the second argument of ‘(.)’, namely ‘group’
    In the expression:
      map
        (\ xs
           -> if (length xs == 1) then
                  Single (head xs)
              else
                  Multiple (length xs) (head xs))
      . group
    In an equation for ‘encodeModified'’:
        encodeModified'
          = map
              (\ xs
                 -> if (length xs == 1) then
                        Single (head xs)
                    else
                        Multiple (length xs) (head xs))
            . group

I can't figure out what's the error by its feedback and I didn't find anything wrong with my method, if someone can help me? thanks.

wwayne
  • 145
  • 1
  • 11

1 Answers1

3

Your code here includes an Eq a constraint in the type of encodeModified, but from the error you posted, I can see that the type you actually have in your file for encodeModified' (note the prime!) does not have that constraint. Add the constraint and you should be good to go.

In the future, it is polite to post the code you are actually having a problem with.

Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • Yeah you're right, and I posted the completed code, I didn't do it because I don't want to confuse others. And why here I have to add constraints on encodeModified', it shouldn't inherit from encodeModified? or I misunderstand something here? thanks – wwayne May 17 '16 at 04:31
  • @wwayne What do you mean by "inherit"? Certainly there is no inheritance in the OO sense in Haskell. – Daniel Wagner May 17 '16 at 04:47
  • My bad, I re-check what ' is in Haskell, it doesn't have special meaning, I misunderstood of it. Anyway, thanks for your patience and answer. – wwayne May 17 '16 at 05:07