From f70caf1972f12b4f94c31eccacc37182601a8d78 Mon Sep 17 00:00:00 2001 From: Dario Sindicic Date: Sat, 6 Oct 2018 11:04:12 +0200 Subject: [PATCH 1/2] roman number converter --- roman/roman.hs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 roman/roman.hs diff --git a/roman/roman.hs b/roman/roman.hs new file mode 100644 index 00000000..e3dde6ad --- /dev/null +++ b/roman/roman.hs @@ -0,0 +1,40 @@ +module Main where + +import Data.Bool + +romanToInt :: Char -> Integer +romanToInt 'I' = 1 +romanToInt 'V' = 5 +romanToInt 'X' = 10 +romanToInt 'L' = 50 +romanToInt 'C' = 100 +romanToInt 'D' = 500 +romanToInt 'M' = 1000 +romanToInt _ = 0 + + +romanToNum :: [Char] -> Integer +romanToNum xs + | length xs == 0 = 0 + | length xs == 1 = romanToInt (head xs) + | otherwise = let fst = romanToInt (head xs) + snd = romanToInt (xs !! 1) + t1 = romanToNum (tail xs) + in if fst < snd then t1 - fst + else t1 + fst + +main :: IO() +main = putStrLn result + +result :: [Char] +result = if final == False then "Your program is wrong" + else "Your program is working!!! " + where final = foldl (&&) True expr + expr :: [Bool] + expr = [e1, e2, e3, e4, e5] + e1 = (romanToNum "XVI") == 16 + e2 = (romanToNum "VI") == 6 + e3 = (romanToNum "CM") == 900 + e4 = (romanToNum "CMXL") == 940 + e5 = (romanToNum "XIII") == 13 + From 8da35faaf4950cc5e9637451956ce37787913c2b Mon Sep 17 00:00:00 2001 From: Dario Sindicic Date: Sat, 6 Oct 2018 11:26:24 +0200 Subject: [PATCH 2/2] root of a number --- binary_search/root_of_a_number.hs | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 binary_search/root_of_a_number.hs diff --git a/binary_search/root_of_a_number.hs b/binary_search/root_of_a_number.hs new file mode 100644 index 00000000..ba615c07 --- /dev/null +++ b/binary_search/root_of_a_number.hs @@ -0,0 +1,12 @@ + +sqrtInt :: Int -> Int +sqrtInt x = sqrtInt' x 0 x + where sqrtInt' :: Int -> Int -> Int -> Int + sqrtInt' x low high + | low >= high = low + | middSq < x = sqrtInt' x (midd+1) high + | middSq > x = sqrtInt' x (low) (midd-1) + | middSq == x = midd + where midd = (high + low) `quot` 2 + middSq = midd*midd +