read :: Int不在Haskell中工作

时间:2016-05-09 16:04:24

标签: haskell

这是问题所在。它看起来很简单

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)

问题是我认为::就像投射一样,我必须把返回类型。解决方案是添加完整的通缉|函数签名instread。

1 个答案:

答案 0 :(得分:3)

read是一个函数(Read a => String -> a类型),因此它不能具有类型Int。你可以read :: String -> Int,或者你可以在list而不是read上设置类型签名,这样你就得到了:

let list :: [Int]
    list = map read (words s)
相关问题