module Exercise01 where import Data.List import Foreign.Marshal.Utils import Test.QuickCheck {-H1.1a)-} myPair :: Integer -> Integer -> Integer myPair x y = (2 ^ y * (2 * x + 1)) - 1 {-H1.1b)-} mySnd :: Integer -> Integer mySnd pair = mySndHelper (pair + 1) 0 mySndHelper :: Integer -> Integer -> Integer mySndHelper pair count | rest /= 0 = count | otherwise = mySndHelper newPair (count + 1) where (newPair, rest) = pair `quotRem` 2 {-H1.1c)-} myFst :: Integer -> Integer myFst pair = myFstHelper (pair + 1) myFstHelper :: Integer -> Integer myFstHelper pair | rest /= 0 = (pair - 1) `quot` 2 | otherwise = myFstHelper newPair where (newPair, rest) = pair `quotRem` 2 {-H1.1d)-} prop_myPair :: Integer -> Integer -> Integer -> Property prop_myPair p x y = p >= 0 && x >= 0 && y >= 0 ==> myPair x y == p {-H2-} digitToEo :: Integer -> String digitToEo 0 = "nul" digitToEo 1 = "unu" digitToEo 2 = "du" digitToEo 3 = "tri" digitToEo 4 = "kvar" digitToEo 5 = "kvin" digitToEo 6 = "ses" digitToEo 7 = "sep" digitToEo 8 = "ok" digitToEo 9 = "nau" {-WETT-} numberToEo :: Integer -> String numberToEo 0 = "nul" numberToEo num = unwords $ words $ numberToEoHelper num 0 repl :: Char -> Char repl c = if c == '.' then ' ' else c numberToEoHelper :: Integer -> Integer -> String numberToEoHelper 0 _ = "" numberToEoHelper num depth | depth <= 2 = jWS 10 $ pluralize depth $ digitToEo $ num `rem` 10 | otherwise = jWS 1000 $ pluralize depth $ numberToEo $ num `rem` 1000 where suffix = words "[unused] dek cent mil miliono miliardo duiliono duiliardo triiliono triiliardo kvariliono kvariliardo kviniliono kviniliardo sesiliono sesiliardo sepiliono sepiliardo okiliono okiliardo nauiliono nauiliardo dekiliono dekiliardo" `genericIndex` depth jWS a b = quot num a `numberToEoHelper` succ depth ++ " " ++ b pluralize _ "nul" = "" pluralize 0 noW = noW pluralize dep "unu" | dep <= 3 = suffix | otherwise = "unu " ++ suffix pluralize 3 noW = noW ++ " mil" pluralize dep noW | dep < 3 = noW ++ suffix | otherwise = noW ++ " " ++ suffix ++ "j" {-TTEW-}