import Data.Number.CReal
-- mayorPrefijoComún
-- =================
-- 1ª definición
mayorPrefijoComun :: Eq a => [a] -> [a] -> [a]
mayorPrefijoComun [] _ = []
mayorPrefijoComun _ [] = []
mayorPrefijoComun (x:xs) (y:ys)
| x == y = x : mayorPrefijoComun xs ys
| otherwise = []
-- 2ª definición
mayorPrefijoComun2 :: Eq a => [a] -> [a] -> [a]
mayorPrefijoComun2 xs ys =
[x | (x,y) <- takeWhile (\(x,y) -> x == y) (zip xs ys)]
-- 3ª definición
mayorPrefijoComun3 :: Eq a => [a] -> [a] -> [a]
mayorPrefijoComun3 xs ys =
[x | (x,y) <- takeWhile (uncurry (==)) (zip xs ys)]
-- 4ª definición
mayorPrefijoComun4 :: Eq a => [a] -> [a] -> [a]
mayorPrefijoComun4 xs =
map fst . takeWhile (uncurry (==)) . zip xs
-- 5ª definición
mayorPrefijoComun5 :: Eq a => [a] -> [a] -> [a]
mayorPrefijoComun5 =
((map fst . takeWhile (uncurry (==))) .) . zip
-- precisionPi
-- ===========
-- 1ª definición
precisionPi :: Double -> Int
precisionPi x =
length (mayorPrefijoComun xs ys) - 1
where xs = show x
ys = show pi
-- 2ª definición
precisionPi2 :: Double -> Int
precisionPi2 =
pred . length . mayorPrefijoComun (show pi) . show
-- precisionPiCR
-- =============
precisionPiCR :: CReal -> Int
precisionPiCR = aux 50
where aux n x | p < n-1 = p
| otherwise = aux (n+50) x
where p = precisionPiCRHasta n x
-- (precisionPiCRHasta n x) es la precisión de pi acotada por n. Por
-- ejemplo,
-- precisionPiCRHasta 1 3.14 == 2
-- precisionPiCRHasta 5 3.14 == 3
-- precisionPiCRHasta 5 3.142 == 3
-- precisionPiCRHasta 5 3.141 == 4
precisionPiCRHasta :: Int -> CReal -> Int
precisionPiCRHasta n x =
length (mayorPrefijoComun xs ys) - 1
where xs = showCReal n x
ys = showCReal n pi