@@ -1,7 +1,20 @@
1import System
1import System
3+{- Toy program that outputs the n first digits of Pi.
5+ Inspired from http://www.haskell.org/haskellwiki/Shootout/Pidigits.
6+ The original ns and str lazy lists have been replaced by strict functions.
8+ 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.
11+ For reference, the Haskell version runs in 0m0.230s when printing to /dev/null.
12+ It almost runs in constant space.
3data F = mkF Integer Integer Integer
15data F = mkF Integer Integer Integer
17+-- Prints the list of digits by groups of 10
5loop : Nat -> Nat -> List Integer -> IO()
18loop : Nat -> Nat -> List Integer -> IO()
6loop n k' Nil = putStrLn $ (pack $ Vect.replicate n ' ') ++ "\t:" ++ show k'
19loop n k' Nil = putStrLn $ (pack $ Vect.replicate n ' ') ++ "\t:" ++ show k'
7loop Z k' xs = do putStrLn ("\t:"++show k')
20loop Z k' xs = do putStrLn ("\t:"++show k')
@@ -18,6 +31,7 @@ flr x (mkF q r t) = (q*x + r) `div` t
18comp : F -> F -> F
31comp : F -> F -> F
19comp (mkF q r t) (mkF u v x) = mkF (q*u) (q*v+r*x) (t*x)
32comp (mkF q r t) (mkF u v x) = mkF (q*u) (q*v+r*x) (t*x)
34+-- Returns the list of digits of pi. Memory hungry.
21str : F -> Integer -> Nat -> List Integer
35str : F -> Integer -> Nat -> List Integer
22str _ _ Z = Nil
36str _ _ Z = Nil
23str z k (S n) = if(y == flr 4 z)
37str z k (S n) = if(y == flr 4 z)