[Haskell-beginners] IO operations

Alexander Klink alexander at klink.name
Thu Aug 13 11:24:00 UTC 2020


On Thu, Aug 13, 2020 at 01:02:19PM +0200, Alexander Chen wrote:
> module Main where
> 
> allwords :: IO Int
> allwords  = do
>     dict <- readFile "data/dict.txt"
>     return  $ length [x | x <- dict , not $ '-' `elem` x]
>     
> main :: IO Int
> main = do allwords
> 
> error
> 
> * Couldn't match expected type `t0 Char' with actual type `Char'
> * In the second argument of `elem', namely `x'
>   In the second argument of `($)', namely '-' `elem` x
>   In the expression: not $ '-' `elem` x
> 
> could someone tell me what I am doing wrong?

dict is of type String, since it's a String containing the complete
file. That's why the type complaint is that you want to check if '-' (a
Char) is an element of x, which is also a Char, which cannot work.

I asssume what you want is a list of lines, which you can by
applying the lines function, e.g. here:

     return  $ length [x | x <- lines dict , not $ '-' `elem` x]

HTH,
Cheers,
Alex


More information about the Beginners mailing list