on master help sign in
Fix pidigits benchmark
bccb453Paul Koerbitz
benchmarks/pidigits/pidigits.idr+10−10
@@ -1,18 +1,19 @@
1import System
1import System
2+import Data.Vect
2
3
3{- Toy program that outputs the n first digits of Pi.
4{- Toy program that outputs the n first digits of Pi.
4
5
5− Inspired from http://www.haskell.org/haskellwiki/Shootout/Pidigits.
6+ Inspired from http://www.haskell.org/haskellwiki/Shootout/Pidigits.
6 The original ns and str lazy lists have been replaced by strict functions.
7 The original ns and str lazy lists have been replaced by strict functions.
7
8
8− Memory usage seems to be excessive. One of the branches of str is tail recursive, and
9+ Memory usage seems to be excessive. One of the branches of str is tail recursive, and
9 the other one only needs to cons an extra Integer.
10 the other one only needs to cons an extra Integer.
10
11
11− For reference, the Haskell version runs in 0m0.230s when printing to /dev/null.
12+ For reference, the Haskell version runs in 0m0.230s when printing to /dev/null.
12 It almost runs in constant space.
13 It almost runs in constant space.
13-}
14-}
14
15
15−data F = mkF Integer Integer Integer
16+data F = MkF Integer Integer Integer
16
17
17-- Prints the list of digits by groups of 10
18-- Prints the list of digits by groups of 10
18loop : Nat -> Nat -> List Integer -> IO()
19loop : Nat -> Nat -> List Integer -> IO()
@@ -23,19 +24,19 @@ loop (S k) k' (x::xs) = do putStr (show x)
23 loop k (S k') xs
24 loop k (S k') xs
24
25
25fn : Integer -> F
26fn : Integer -> F
26−fn k = mkF k (4*k+2) (2*k+1)
27+fn k = MkF k (4*k+2) (2*k+1)
27
28
28flr : Integer -> F -> Integer
29flr : Integer -> F -> Integer
29−flr x (mkF q r t) = (q*x + r) `div` t
30+flr x (MkF q r t) = (q*x + r) `div` t
30
31
31comp : F -> F -> F
32comp : F -> F -> F
32−comp (mkF q r t) (mkF u v x) = mkF (q*u) (q*v+r*x) (t*x)
33+comp (MkF q r t) (MkF u v x) = MkF (q*u) (q*v+r*x) (t*x)
33
34
34-- Returns the list of digits of pi. Memory hungry.
35-- Returns the list of digits of pi. Memory hungry.
35str : F -> Integer -> Nat -> List Integer
36str : F -> Integer -> Nat -> List Integer
36str _ _ Z = Nil
37str _ _ Z = Nil
37str z k (S n) = if(y == flr 4 z)
38str z k (S n) = if(y == flr 4 z)
38− then y :: str (comp (mkF 10 (-10*y) 1) z ) k n
39+ then y :: str (comp (MkF 10 (-10*y) 1) z ) k n
39 else str (comp z (fn k)) (k+1) (S n)
40 else str (comp z (fn k)) (k+1) (S n)
40 where y = flr 3 z
41 where y = flr 3 z
41
42
@@ -43,10 +44,9 @@ pidigit : IO()
43pidigit = do
44pidigit = do
44 [_,a] <- getArgs
45 [_,a] <- getArgs
45 let n = fromIntegerNat (the Integer (cast a))
46 let n = fromIntegerNat (the Integer (cast a))
46− let l = str (mkF 1 0 1) 1 n
47+ let l = str (MkF 1 0 1) 1 n
47 loop 10 0 l
48 loop 10 0 l
48 return ()
49 return ()
49
50
50main : IO ()
51main : IO ()
51main = pidigit
52main = pidigit
52−