2016-05-09 25 views
0

這是問題所在。它看起來很簡單但read :: Int未在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。

回答

3

read是一個函數(類型Read a => String -> a),所以它不能有類型Int。你可以做read :: String -> Int,或者你可以把一個類型簽名上list而非read,讓您得到:

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