Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

0
redditAnswers = [Either (error, reasons) insults]
Post Body

Jokes aside, I really don't know if this is the right place to ask, so feel free to redirect me somewhere more appropriate if that's the case.

I've very recently picked up haskell, because functional programming really appeals to me as a maths student, and I've been working through these Ninety-Nine Haskell Problems, but I got stuck at Problem 11.

In short, I am asked to encode a list like (AAABCC) to something like ( (3 A) B (3 C) ).

Completely disregarding the data types I'm suggested, I decided to implement a function of this sort:

myEncodeMod :: (Eq a) => [a] -> [Either (Int, a) a]

In my mind, this would take a list and return a list of either pairs (number, element) or just the element.

Here is my relevant code:

myPack :: (Eq a) => [a] -> [[a]]
myPack [] = []
myPack (x:xs) = let (first, rest) = span (==x) xs
                in (x:first) : myPack rest

myEncodeMod :: (Eq a) => [a] -> [Either (Int, a) a]
myEncodeMod xs = map compress $ myPack xs
    where   compress [y] = y
             compress ys = (length ys, head ys)

The myPack function turns a list (AAAAABBC) into ( (AAAAA) (BB) (C) ), if that makes sense.

And here is the error I get when trying to load the file in ghci:

Prelude> :l haskell99.hs 
[1 of 1] Compiling Main             ( haskell99.hs, interpreted )

haskell99.hs:45:18:
    Couldn't match type ‘(Int, t0)’ with ‘Either (Int, a) a’
    Expected type: [Either (Int, a) a]
      Actual type: [(Int, t0)]
    Relevant bindings include
      xs :: [a] (bound at haskell99.hs:45:13)
      myEncodeMod :: [a] -> [Either (Int, a) a]
        (bound at haskell99.hs:45:1)
    In the expression: map compress $ myPack xs
    In an equation for ‘myEncodeMod’:
        myEncodeMod xs
          = map compress $ myPack xs
          where
              compress [y] = y
              compress ys = (length ys, head ys)

haskell99.hs:47:44:
    Occurs check: cannot construct the infinite type: t ~ (Int, t)
    Expected type: [t]
      Actual type: [(Int, t)]
    Relevant bindings include
      ys :: [(Int, t)] (bound at haskell99.hs:47:22)
      compress :: [(Int, t)] -> (Int, t) (bound at     haskell99.hs:46:13)
    In the first argument of ‘head’, namely ‘ys’
    In the expression: head ys
Failed, modules loaded: none.

It looks to me as if I messed up with the Either thing, but I con't exactly find my mistake.

I'm sure it's some trivial notation issue, but any help would be very much appreciated, thanks!

Author
Account Strength
100%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,881
Link Karma
1,039
Comment Karma
12,809
Profile updated: 2 hours ago
Posts updated: 7 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago