【发布时间】:2014-09-17 13:14:31
【问题描述】:
以下代码
import Control.Applicative
import Control.Arrow
import Data.List.Split
main :: IO ()
main = do
ints <- getNumberLine
integers <- getNumberLine
print $ foo ints integers
getNumberLine = readDataLine <$> getLine
readDataLine :: Read a => String -> [a]
readDataLine = splitOn " " >>> map read
foo :: [Int] -> [Integer] -> String
foo _ _ = "hello"
给出这个错误信息:
GetNumberLine.hs:9:22:
Couldn't match type `Int' with `Integer'
Expected type: [Integer]
Actual type: [Int]
In the second argument of `foo', namely `integers'
In the second argument of `($)', namely `foo ints integers'
In a stmt of a 'do' block: print $ foo ints integers
Failed, modules loaded: none.
只有在我创建第二个 getNumberLine 函数时才有效:
import Control.Applicative
import Control.Arrow
import Data.List.Split
main :: IO ()
main = do
ints <- getNumberLine
integers <- getNumberLine2
print $ foo ints integers
getNumberLine = readDataLine <$> getLine
getNumberLine2 = readDataLine <$> getLine
readDataLine :: Read a => String -> [a]
readDataLine = splitOn " " >>> map read
foo :: [Int] -> [Integer] -> String
foo _ _ = "hello"
我觉得很丑。
为什么这是必要的?还有更好的方法吗?
【问题讨论】: