Created
May 5, 2014 15:05
-
-
Save MgaMPKAy/b489d533582ca4ee107f to your computer and use it in GitHub Desktop.
PatternSynonyms example
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
{-# LANGUAGE ViewPatterns #-} | |
{-# LANGUAGE PatternSynonyms #-} | |
import System.Environment | |
import Data.Ratio | |
import Control.Applicative ((<$>)) | |
viewRatio x = (numerator x, denominator x) | |
pattern p :% q <- (viewRatio -> (p, q)) | |
main = do | |
[p, q] <- map read <$> getArgs | |
let x = p % q | |
putStr $ show x ++ ": " | |
mapM_ (putStr . (++" ") . show) $ fib x | |
putStrLn "" | |
fib (1 :% q) = [q] | |
fib x@(p :% q) = let s = q `div` p + 1 | |
in s : fib (x - (1 % s)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment