diff options
Diffstat (limited to 'Haskell')
| -rw-r--r-- | Haskell/mooc-exercises.hs | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/Haskell/mooc-exercises.hs b/Haskell/mooc-exercises.hs new file mode 100644 index 0000000..879ec8f --- /dev/null +++ b/Haskell/mooc-exercises.hs @@ -0,0 +1,147 @@ +-- This are my solutions to the exercises from https://github.com/moocfi/haskell-mooc + +import Data.List + +--------- +-- Set.01 +-- https://github.com/moocfi/haskell-mooc/blob/master/exercises/Set1.hs + +-- Ex. 1 + +one :: Integer -- function declaration: types of arguments and output +one = 1 -- function definition + +two :: Integer +two = 2 + +-- Ex. 2 + +double :: Integer -> Integer +double x = x * 2 + +-- Ex. 3 + +quadruple :: Integer -> Integer +quadruple x = double (double x) + +-- Ex. 4 + +distance :: Double -> Double -> Double -> Double -> Double +-- exponentiation operator: ^ for integers, ^^ for fractionals and ** for floating point +distance x1 y1 x2 y2 = sqrt (abs(x1 - x2)**2 + abs(y1 - y2)**2) + +-- Ex. 5 + +eeny :: Integer -> String +eeny x = if even x + then "eeny" + else "meeny" + +-- Ex. 6 + +checkPassword :: String -> String +checkPassword password = if password == "swordfish" || password == "mellon" + then "You're in." + else "ACCESS DENIED!" + +-- Ex. 7 + +postagePrice :: Int -> Int +postagePrice weight = if weight > 5000 then 6000 + else if weight > 500 then 300 + weight * 1 + else 250 + +-- Ex. 8 + +isZero :: Int -> Bool +isZero 0 = True -- this is pattern matching +isZero x = False + +-- Ex. 9 + +sumTo :: Int -> Int +sumTo 1 = 1 +sumTo n = n + sumTo (n - 1) + +-- Ex. 10 + +power :: Int -> Int -> Int +power n 1 = n +power n k = n * power n (k - 1) + +-- Ex. 11 + +ilog3 :: Int -> Int +ilog3 0 = 0 +ilog3 n = 1 + ilog3 (div n 3) + +---------- +-- Set.02a +-- https://github.com/moocfi/haskell-mooc/blob/master/exercises/Set2a.hs + +-- Functions you'll need: head, tail, take, drop, length, null + +-- Ex. 1 + +years = [1982, 2004, 2020] + +-- Ex. 2 + +takeFinal :: Int -> [a] -> [a] -- a is the Haskell equivalent of generics (T) in C# +takeFinal n xs + | n > length xs = xs + | otherwise = drop (length xs - n) xs + +-- Ex. 3 + +updateAt :: Int -> a -> [a] -> [a] +-- elements before i + x + elemenets after i ; ++ concatenates arrays +updateAt i x xs = take i xs ++ [x] ++ drop (i + 1) xs + +-- Ex. 4 + +substring :: Int -> Int -> String -> String +substring i j s = drop i (take j s) + +-- Ex. 5 + +isPalindrome :: String -> Bool +isPalindrome s = s == reverse s + +-- Ex. 6 + +palindromify :: String -> String +palindromify s = if isPalindrome s then s -- can easily make it: if s == reverse s then s , but decided this way was more readable + else palindromify (take (length s - 2) (drop 1 s)) + +-- Ex. 7 + +safeDiv :: Int -> Int -> String +safeDiv x y + | y == 0 = "Nothing" + | otherwise = "Just " ++ show (div x y) -- show casts the value to string + +-- Ex. 8 + +greet :: String -> Maybe String -> String -- maybe means the value can (String) or can not be given (Nothing) ; you need to use pattern matching +greet firstName Nothing = "Hello, " ++ firstName ++ "!" +greet firstName (Just lastName) = "Hello, " ++ firstName ++ " " ++ lastName ++ "!" + +-- Ex. 9 + +safeIndex :: [a] -> Int -> Maybe a +safeIndex xs i = if 0 > i || i >= length xs then Nothing + else Just (xs !! i) + +-- Ex. 10 + +eitherDiv :: Int -> Int -> Either String Int -- a value can be the left one or the right one (specified when given) +eitherDiv x 0 = Left (show x ++ "/0") +eitherDiv x y = Right (div x y) + +-- Ex. 11 + +addEithers :: Either String Int -> Either String Int -> Either String Int +addEithers (Right x) (Right y) = Right (x + y) +addEithers (Right x) (Left y) = Left y +addEithers (Left x) (Right y) = Left x |
