|
@@ -1,6 +1,7 @@
|
|
|
module Utils where
|
|
module Utils where
|
|
|
import Data.Function
|
|
import Data.Function
|
|
|
import Data.Maybe
|
|
import Data.Maybe
|
|
|
|
|
+ import Data.Ratio
|
|
|
import Data.List
|
|
import Data.List
|
|
|
import Data.Char
|
|
import Data.Char
|
|
|
|
|
|
|
@@ -241,3 +242,44 @@ module Utils where
|
|
|
go' :: Show a => [a] -> Int -> Bool
|
|
go' :: Show a => [a] -> Int -> Bool
|
|
|
go' [] _ = False
|
|
go' [] _ = False
|
|
|
go' (x:xs) i = trace (printf "%5d -> %s" i (show x)) (go' xs (i+1))
|
|
go' (x:xs) i = trace (printf "%5d -> %s" i (show x)) (go' xs (i+1))
|
|
|
|
|
+
|
|
|
|
|
+ slice :: Int -> Int -> [a] -> [a]
|
|
|
|
|
+ slice b s l = drop b l |> take s
|
|
|
|
|
+
|
|
|
|
|
+ howManyFit :: Int -> Int -> Int
|
|
|
|
|
+ --howManyFit x y | trace (printf "howManyFit %3d %3d" x y) False = undefined
|
|
|
|
|
+ howManyFit x y = if w == 0
|
|
|
|
|
+ then 1
|
|
|
|
|
+ else (if w > 0
|
|
|
|
|
+ then 1 + (howManyFit x w)
|
|
|
|
|
+ else 0)
|
|
|
|
|
+ where w = y-x
|
|
|
|
|
+
|
|
|
|
|
+ tW f (x:xs) = case f x of
|
|
|
|
|
+ True -> tW f xs
|
|
|
|
|
+ False -> x
|
|
|
|
|
+
|
|
|
|
|
+ longDiv :: Ratio Int -> String
|
|
|
|
|
+ longDiv r = if dn == nmRaw then "1" else pred ++ (go' nm False 5000)
|
|
|
|
|
+ where
|
|
|
|
|
+ nmRaw = numerator r
|
|
|
|
|
+ dn = denominator r
|
|
|
|
|
+ hMInc = iterate (+1) 1 |> tW (\x -> (howManyFit dn (nmRaw*(10^x))) == 0)
|
|
|
|
|
+ nm = nmRaw * (10^hMInc)
|
|
|
|
|
+ pred = case hMInc of
|
|
|
|
|
+ 1 -> "0."
|
|
|
|
|
+ 2 -> "0.0"
|
|
|
|
|
+ 3 -> "0.00"
|
|
|
|
|
+ 4 -> "0.000"
|
|
|
|
|
+ _ -> ""
|
|
|
|
|
+
|
|
|
|
|
+ go' :: Int -> Bool -> Int -> [Char]
|
|
|
|
|
+ go' 0 _ _ = []
|
|
|
|
|
+ go' _ _ 0 = []
|
|
|
|
|
+ go' x z n
|
|
|
|
|
+ -- | trace (printf "go' %3d %3d | hmf -> %3d" x n hmf) False = undefined
|
|
|
|
|
+ | hmf == 0 && z = ['0'] ++ (go' x False (n-1))
|
|
|
|
|
+ | otherwise = if hmf == 0
|
|
|
|
|
+ then go' (x * 10) True (n)
|
|
|
|
|
+ else [(intToDigit hmf)] ++ (go' (x-(dn*hmf)) False (n-1))
|
|
|
|
|
+ where hmf = howManyFit dn x
|