Created
May 17, 2013 20:55
-
-
Save ijp/5601903 to your computer and use it in GitHub Desktop.
This file contains 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
import Data.List(intercalate, iterate) | |
import Data.Ratio | |
unfold :: (a -> Bool) -> (a -> b) -> (a -> a) -> a -> [b] | |
unfold stop mapper next seed | |
= map mapper $ takeWhile (not . stop) $ iterate next seed | |
data Continued = CFrac Integer [Integer] | |
deriving (Eq) | |
instance Show Continued where | |
show (CFrac c cs) = "[" ++ show c ++ ";" ++ cs' ++ "]" | |
where cs' = intercalate "," (map show cs) | |
continued :: Rational -> Continued | |
continued rat = CFrac c (cont cs) | |
where (c,cs) = properFraction rat | |
cont rat = unfold (== 0) (floor . recip) (fractionalPart . recip) rat | |
fractionalPart :: Rational -> Rational | |
fractionalPart frac = frac - fromIntegral (floor frac) | |
rational :: Continued -> Rational | |
rational (CFrac c cs) = c' + foldr step 0 cs' | |
where step x prev = recip (x + prev) | |
c' = fromIntegral c | |
cs' = map fromIntegral cs | |
tests = and [(9 % 16) === CFrac 0 [1,1,3,2], | |
(415 % 93) === CFrac 4 [2,6,7], | |
(23456 % 10000) === CFrac 2 [2,1,8,2,1,1,4]] | |
where a === b = continued a == b | |
tests2 = and (map sane [12, (137 % 1600), (49 % 8), (12 % 13)]) | |
where sane x = x == rational (continued x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment