Aftershock
Aftershock

Reputation: 5351

read::Int not working in Haskell in example

Here is the problem. It looks simple yet

main = do 
   s <- getContents
   let list = map (read::Int) (words s)
   print list

    Couldn't match expected type `Int' with actual type `String -> a0'
    Probable cause: `read' is applied to too few arguments
    In the first argument of `map', namely `(read :: Int)'
    In the expression: map (read :: Int) (words s)

The problem was that I thought :: is like casting and I have to put the return type. The solution was to add full wanted |function signature instread.

Upvotes: 0

Views: 135

Answers (1)

sepp2k
sepp2k

Reputation: 370425

read is a function (of type Read a => String -> a), so it can't have type Int. You could do read :: String -> Int, or you could put a type signature on list rather than read, so you get:

let list :: [Int]
    list = map read (words s)

Upvotes: 3

Related Questions