Created
January 26, 2010 18:05
-
-
Save zard1989/287053 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| asInt :: String -> Int | |
| asInt xs = loop 0 xs | |
| loop acc [] = acc | |
| loop acc (x:xs) = let acc' = acc * 10 + digitToInt x | |
| in loop acc' xs | |
| loopF :: Double -> String -> Double | |
| loopF acc [] = acc | |
| loopF acc (x:xs) = let acc' = (acc + fromIntegral (digitToInt x))*0.1 | |
| in loopF acc' xs | |
| asDecimal :: String -> Double | |
| asDecimal xs = loopF 0 (reverse xs) | |
| asDouble :: String -> Double | |
| asDouble xs = | |
| if null (filter (=='.') xs) | |
| then fromIntegral $ asInt xs | |
| else let (intPart, '.':decPart) = break (=='.') xs | |
| in (fromIntegral $ asInt intPart) + (asDecimal decPart) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment