Skip to content

Instantly share code, notes, and snippets.

@danyx23
Last active May 23, 2016 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danyx23/c7b40c959f816bcd2ed9f2e56293025b to your computer and use it in GitHub Desktop.
Save danyx23/c7b40c959f816bcd2ed9f2e56293025b to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings, GeneralizedNewtypeDeriving #-}
module Text.Fractions where
import Control.Applicative
import Data.Ratio ((%))
import Text.Trifecta
import Test.Hspec
badFraction = "1/0"
alsoBad = "10"
shouldWork = "1/2"
shouldAlsoWork = "2/1"
newtype PitchClass = PitchClass Int deriving (Show, Num)
addPitchClasses'' x y =
x + y
addPitchClasses :: PitchClass -> PitchClass -> PitchClass
addPitchClasses (PitchClass x) (PitchClass y) =
PitchClass (x + y)
parsePitchClass :: Parser PitchClass
parsePitchClass =
PitchClass . fromInteger <$> integer
parsePitchClass' :: Parser PitchClass
parsePitchClass' = do
x <- integer
return (PitchClass $ fromInteger x)
parseFraction :: Parser Rational
parseFraction = do
numerator <- decimal
char '/'
denominator <- decimal
case denominator of
0 -> fail "Denominator cannot be zero"
_ -> return (numerator % denominator)
parseFractionInParenthesis =
parens parseFraction
maybeSuccess :: Result a -> Maybe a
maybeSuccess (Success a) = Just a
maybeSuccess _ = Nothing
main :: IO ()
main = hspec $ do
describe "test" $
it "can do this" $ do
let r' = 1
print r'
r' `shouldBe` 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment