Skip to content

Instantly share code, notes, and snippets.

@MgaMPKAy
Created May 5, 2014 15:05
Show Gist options
  • Save MgaMPKAy/b489d533582ca4ee107f to your computer and use it in GitHub Desktop.
Save MgaMPKAy/b489d533582ca4ee107f to your computer and use it in GitHub Desktop.
PatternSynonyms example
{-# 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