lines, words, unlines, unwords, reverse, and, or,
any, all, elem, notElem, lookup,
sum, product, maximum, minimum, concatMap,
- zip, zip3, zipWith, zipWith3, unzip, unzip3
+ zip, zip3, zipWith, zipWith3, unzip, unzip3,
+
+ -- non-standard, but hidden when creating the Prelude
+ -- export list.
+ takeUInt
+
) where
import {-# SOURCE #-} PrelErr ( error )
#else /* hack away */
take :: Int -> [b] -> [b]
-take (I# n#) xs = takeUInt n# xs
+take (I# n#) xs = takeUInt n# xs []
-- The general code for take, below, checks n <= maxInt
-- No need to check for maxInt overflow when specialised
-- at type Int or Int# since the Int must be <= maxInt
-takeUInt :: Int# -> [b] -> [b]
-takeUInt n xs
- | n >=# 0# = take_unsafe_UInt n xs
+takeUInt :: Int# -> [b] -> [b] -> [b]
+takeUInt n xs rs
+ | n >=# 0# = take_unsafe_UInt n xs rs
| otherwise = errorNegativeIdx "take"
-take_unsafe_UInt :: Int# -> [b] -> [b]
-take_unsafe_UInt 0# _ = []
-take_unsafe_UInt m ls =
+take_unsafe_UInt :: Int# -> [b] -> [b] -> [b]
+take_unsafe_UInt 0# _ rs = rs
+take_unsafe_UInt m ls rs =
case ls of
- [] -> ls
- (x:xs) -> x : take_unsafe_UInt (m -# 1#) xs
+ [] -> rs
+ (x:xs) -> x : take_unsafe_UInt (m -# 1#) xs rs
drop :: Int -> [b] -> [b]
drop (I# n#) ls
tn = 10^n
in if x >= tn then norm ee (x/tn) (e+n) else norm (ee-1) x e
-drop0 :: String -> String
-drop0 "" = ""
-drop0 (c:cs) = c : fromMaybe [] (dropTrailing0s cs) --WAS (yuck): reverse (dropWhile (=='0') (reverse cs))
+prR :: Int -> Rational -> Int -> String
+prR n r e | r < 1 = prR n (r*10) (e-1) -- final adjustment
+prR n r e | r >= 10 = prR n (r/10) (e+1)
+prR n r e0
+ | e > 0 && e < 8 = takeN e s ('.' : drop0 (drop e s) [])
+ | e <= 0 && e > -3 = '0': '.' : takeN (-e) (repeat '0') (drop0 s [])
+ | otherwise = h : '.' : drop0 t ('e':show e0)
+ where
+ s@(h:t) = show ((round (r * 10^n))::Integer)
+ e = e0+1
+
+ takeN (I# n#) ls rs = takeUInt n# ls rs
+
+drop0 :: String -> String -> String
+drop0 [] rs = rs
+drop0 (c:cs) rs = c : fromMaybe rs (dropTrailing0s cs) --WAS (yuck): reverse (dropWhile (=='0') (reverse cs))
where
dropTrailing0s [] = Nothing
dropTrailing0s ('0':xs) =
Nothing -> Just [x]
Just ls -> Just (x:ls)
-prR :: Int -> Rational -> Int -> String
-prR n r e | r < 1 = prR n (r*10) (e-1) -- final adjustment
-prR n r e | r >= 10 = prR n (r/10) (e+1)
-prR n r e0 =
- let s = show ((round (r * 10^n))::Integer)
- e = e0+1
- in if e > 0 && e < 8 then
- take e s ++ "." ++ drop0 (drop e s)
- else if e <= 0 && e > -3 then
- "0." ++ take (-e) (repeat '0') ++ drop0 s
- else
- head s : "."++ drop0 (tail s) ++ "e" ++ show e0
\end{code}
[In response to a request for documentation of how fromRational works,
Just dec ->
let dec' = max dec 1 in
case is of
- [0] -> '0':'.':take dec' (repeat '0') ++ "e0"
+ [0] -> '0' :'.' : take dec' (repeat '0') ++ "e0"
_ ->
let
(ei,is') = roundTo base (dec'+1) is