Haskell:类型,时间,IO ......一般问题

时间:2015-11-08 06:00:51

标签: haskell time io

叹息...... Haskell的类型系统真的踢我的屁股......特别是当投入IO问题时。

我试图测量一些并行化代码的性能加速。我需要使用不同的参数多次运行它(例如i = 30),然后计算平均值等。

因此,我在并行化代码之前和之后开始和结束计时器("模拟"在下面的代码中)。

simulate :: Int -> Int -> [Double]
simulate numbuyers groupsize =
    let buyers = initTraders numbuyers minimum_price maximum_price
        sellers = initTraders numbuyers minimum_price maximum_price
        prices = doParallelTrades sellers buyers groupsize
    in prices


runNtimes :: Int -> Int -> Int -> IO [(Double, Int, Double)]
runNtimes numbuyers groupsize i
    | i==0 = []
    | otherwise = do
        start <- getCurrentTime
        let prices = simulate numbuyers groupsize
        end <- getCurrentTime
        let dtime = realToFrac( diffUTCTime end start)
        return (average prices, length prices, dtime):(runNtimes numbuyers groupsize i-1)

runNtimes函数给我一些错误(我已经验证了模拟函数的工作原理):

Couldn't match expected type ‘IO [(Double, Int, Double)]’
                with actual type ‘[t0]’
    In the expression: []

zitraders-threaded.hs:162:21:
    No instance for (Fractional t1) arising from a use of ‘realToFrac’
    The type variable ‘t1’ is ambiguous
    Relevant bindings include
      dtime :: t1 (bound at zitraders-threaded.hs:162:13)

zitraders-threaded.hs:163:9:
    Couldn't match expected type ‘IO [(Double, Int, Double)]’
                with actual type ‘[m0 (Double, Int, t1)]’
    Relevant bindings include
      dtime :: t1 (bound at zitraders-threaded.hs:162:13)

zitraders-threaded.hs:163:56:
    Couldn't match expected type ‘[m0 (Double, Int, t1)]’
                with actual type ‘IO [(Double, Int, Double)]’
    Relevant bindings include
      dtime :: t1 (bound at zitraders-threaded.hs:162:13)

我甚至不知道如何解析这些错误,除非我非常确定我缺乏理解类型和IO ... t0? T1? M0?什么?我在堆栈交换上的其他地方读取使用realToFrac从UTC时间获得一些计算上可用的类型但是考虑到上面的错误,我不确定它们是否正在工作 - 这些错误并没有提供太多信息它是如何不起作用的。令人沮丧。

我还没到达那个部分,但是我在那里用#34; runNtimes&#34;函数 - 基本上只计算列表元素的平均值和标准差(然后打印出来) - 但是我希望返回IO类型也会给我带来麻烦。

0 个答案:

没有答案