在解决一些项目Euler问题以学习Haskell(所以目前我是一个完全初学者)时,我遇到了问题12.我写了这个(天真的)解决方案:
--Get Number of Divisors of n
numDivs :: Integer -> Integer
numDivs n = toInteger $ length [ x | x<-[2.. ((n `quot` 2)+1)], n `rem` x == 0] + 2
--Generate a List of Triangular Values
triaList :: [Integer]
triaList = [foldr (+) 0 [1..n] | n <- [1..]]
--The same recursive
triaList2 = go 0 1
where go cs n = (cs+n):go (cs+n) (n+1)
--Finds the first triangular Value with more than n Divisors
sol …Run Code Online (Sandbox Code Playgroud) 我是Haskell的新手,我希望能够计算给定函数调用或代码片段的运行时间.
在Clojure中我可以使用' 时间 ':
user=> (time (apply * (range 2 10000)))
"Elapsed time: 289.795 msecs"
2846259680917054518906413212119868890148051...
Run Code Online (Sandbox Code Playgroud)
在Scala中,我可以自己定义函数:
scala> def time[T](code : => T) = {
| val t0 = System.nanoTime : Double
| val res = code
| val t1 = System.nanoTime : Double
| println("Elapsed time " + (t1 - t0) / 1000000.0 + " msecs")
| res
| }
time: [T](=> T)T
scala> time((1 to 10000).foldLeft(1:BigInt)(_*_))
Elapsed time 274.292224 msecs
res0: BigInt = 284625968091705451...
Run Code Online (Sandbox Code Playgroud)
如何在Haskell中编写我的Scala函数或Clojure的'time'的等价物?我在Hackage上找到的System.TimeIt模块不够通用,因为它仅在测量IO计算时才有效.所以timeIt(4 …