Last active
May 23, 2016 17:06
-
-
Save danyx23/c7b40c959f816bcd2ed9f2e56293025b 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
{-# 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