Merche
Merche

Reputation: 129

Couldn't match type `[Char]' with `Char'

I have the following code:

nonstopfreq :: Eq a => [a] -> [(a,Int)] -> [(a,Int)]
nonstopfreq (x : xs) (y : ys) = nonstopfreq xs (filter (\y -> not (x == fst y)) ys) 

where the arguments are:

  1. list of String.
  2. tuple [([String],Int)]

but I get the following error, because?

Couldn't match type `[Char]' with `Char'
Expected type: [(String, Int)]
  Actual type: [([String], Int)]
In the second argument of `nonstopfreq', namely `aPrimerB'
In the expression: nonstopfreq lStopWords aPrimerB
In an equation for `aSegon':
    aSegon = nonstopfreq lStopWords aPrimerB

What I am trying to eliminate the elements listed and the tuple.

AprimerB is: [(["the"],1818),(["and"],940),(["to"],809),(["a"],690),(["of"],631),(["it"],545)‌​,(["she"],542),(["said"],462),(["you"],435),(["in"],431)]. The other argument, lStopWords, has type as obtained from words:

   do
     ...
     stopWords <- hGetContents fileStopWords
     let lStopWords = words stopWords

What I try is to remove AprimerB items shown in LstopWords.

Upvotes: 1

Views: 2084

Answers (1)

leftaroundabout
leftaroundabout

Reputation: 120711

Well, the problem is that lStopWords is simply a list of strings (of words, in this particular case) – i.e., [a] ~ [String] or a ~ String. That means the second argument with its general type [(a, Int)] becomes [(String, Int)]. However, you've passed

aPrimerB = [(["the"],1818), (["and"],940) ... ]

where each of the strings is contained in a singleton list. So the type of this is [([String], Int)]. Since each list has just one argument, you can actually change it easily to

aPrimerB :: [(String, Int)]
aPrimerB = [("the",1818), ("and",940) ... ]

which will get rid of that error.

Alternatively, you can also wrap every element of the words list in another singleton-list layer.

   let lStopWords :: [[String]]
       lStopWords = map (:[]) $ words stopWords

Then you can keep the old definition of aPrimerB, because now a ~ [String]. The result would be basically the same, but the singleton lists seem pretty useless here.


Note that even with that error fixed, nonstopfreq will still not work: there is no base case, so it will crash when either of the lists is empty.

Upvotes: 1

Related Questions