看起来[1,2,3]和[1..3]之间存在奇怪的区别。
在下面使用runghc打印出一个" True"然后永远挂起:为什么? (我使用ghc 7.8.3)
module Main where
import Data.Functor((<$>))
import Data.Time.Clock(DiffTime)
import Data.Binary(Binary(..), encode, decode, Get)
import Data.Int(Int64)
instance Binary DiffTime where
put x = put (truncate (x * 10^12) :: Int64)
get = ((/ 10^12) . fromIntegral) <$> (get :: Get Int64)
prop_getput_difftime :: DiffTime -> Bool
prop_getput_difftime x = x == ((decode . encode $ x) :: DiffTime)
explicit :: [DiffTime]
explicit = [1,2,3,4,5,6,7,8,9,10]
elipsis :: [DiffTime]
elipsis = [1..10]
main :: IO ()
main = do
print $ all prop_getput_difftime explicit
print $ all prop_getput_difftime elipsis -- diverges!
注意,上面的Diffary二进制实现是错误的;但这不是重点。
答案 0 :(得分:33)
感谢#haskell的otulp,原因如下:
Prelude> take 3 [1..2] :: [Data.Time.DiffTime]
[1s,1.000000000001s,1.000000000002s]
由于Enum
DiffTime
实例,会发生这种情况
将elipsis
更改为此可解决问题:
elipsis = map fromIntegral ([1..10] :: [Int])