IO operations

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

IO operations

Alexander Chen
Hi,

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?

thanks!

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO operations

Tobias Brandt-3

Hey Alexander,

dict has type String. Therefore, x is of type Char, but elem '-' is of type [Char] -> Bool (simplified). Hence, you get a type mismatch. If you just want to count every character except '-', then you could just you '-' /= x.

Cheers

Tobias

On 8/13/20 1:02 PM, Alexander Chen wrote:
Hi,

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?

thanks!

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO operations

Francesco Ariis
In reply to this post by Alexander Chen
Hello Alexander

Il 13 agosto 2020 alle 13:02 Alexander Chen ha scritto:
> allwords :: IO Int
> allwords  = do
>     dict <- readFile "data/dict.txt"
>     return  $ length [x | x <- dict , not $ '-' `elem` x]

`elem` has type:

    λ> :t elem
    elem :: Eq a => a -> [a] -> Bool
    -- almost

You are comparing '-' (a `Char`) with `x` (another `Char`), hence you
only need `==`. Does this make sense?
—F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO operations

Alexander Klink
In reply to this post by Alexander Chen

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
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: IO operations

鲍凯文
In reply to this post by Alexander Chen
Hi,

> * 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 

Hope the other answers helped. FWIW, just wanted to point out that for me these kinds of error messages sometimes used to be confusing since the type of `elem` is `(Foldable t, Eq a) => a -> t a -> Bool`, so the typechecker tells you it wants to unify `t0 Char` (where `t0` is something `Foldable`) with `Char`. As Mr. Klink wrote, it looks like you wanted `t0` to be `[]`; I used to get confused since I was only working with lists and the extra `Foldable` genericity made it harder to see that I was still just working with lists.

Best,

toz

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners