import Data.List (foldr, unfoldr) -- Tasks ---------------------------------------------- -- Level 1 ---------------------------------------------- -- 1.1: -- rectanglePerimeter 3 4 == 14.0 rectanglePerimeter :: Double -> Double -> Double rectanglePerimeter a b = a*2 + b*2 -- 1.2 -- isVowel 'e' == True -- isVowel 'Q' == False isVowel :: Char -> Bool isVowel x | x == 'a' = True | x == 'e' = True | x == 'i' = True | x == 'o' = True | x == 'u' = True | otherwise = False -- 1.3 -- average3 1 2 3 == 2 average3 :: Int -> Int -> Int -> Int average3 a b c = sum `div` 3 where sum = a + b +c -- 1.4 -- bmiCategory 22.0 == "Normálna váha" -- bmiCategory 31.5 == "Obezita" bmiCategory :: Double -> String bmiCategory bmi | bmi < 18.5 = "podvaha" | bmi >= 18.5 && bmi <= 25 = "normalna vaha" | bmi > 25 && bmi <= 30 = "nadvaha" | otherwise = "obezita" -- 1.5 -- shippingCost 0.5 == 3.0 -- shippingCost 7.0 == 10.0 -- shippingCost :: Double -> Double -- nerobim lebo je to to iste ako predosle ---------------------------------------------- -- Level 2 ---------------------------------------------- -- 2.1: -- rotateLeft 2 [1, 2, 3, 4, 5] == [3, 4, 5, 1, 2] -- rotateLeft 0 [1, 2, 3] == [1, 2, 3] rotateLeft :: Int -> [a] -> [a] rotateLeft x array | x <= length array = drop x array ++ take x array | otherwise = drop k array ++ take k array where k = x `mod` length array -- 2.2 -- myReverse [1, 2, 3] == [3, 2, 1] myReverse :: [a] -> [a] myReverse = reverse -- 2.3 -- removeAll 'a' "banana" == "bnn" -- removeAll 3 [1, 3, 2, 3, 3, 4] == [1, 2, 4] removeAll :: Eq a => a -> [a] -> [a] removeAll letter array = filter (/= letter) array -- 2.4 -- evenSquares [1, 2, 3, 4, 5, 6] == [4, 16, 36] evenSquares :: [Int] -> [Int] evenSquares xs = map (^2) (filter even xs) -- 2.5 -> Neviem -- totalScore 'A' [("Adam", 10), ("Alica", 20), ("Boris", 5)] == 30 -- totalScore :: Char -> [(String, Int)] -> Int -- totalScore s xs = sum (map snd matching) -- where -- matching = filter checkName xs -- checkName (name, _) = name == elem s name -- 2.6 _> Neviem -- translate [('a', '4'), ('e', '3')] "ahoj svete" == "4hoj sv3t3" -- translate :: [(Char, Char)] -> String -> String -- translate replacement sentence = unwords (map replaceWord (words sentence)) -- where -- replaceWord w = case lookup w replacement of -- Just new -> new -- Nothing -> w ---------------------------------------------- -- Level 3 ---------------------------------------------- -- 3.1: -> furt vracia true nechapem -- listOr [False, False, True] == True -- listOr [False, False] == False listOr :: [Bool] -> Bool listOr = foldr (||) True -- 3.2 -> ale nechapem , preco \_ preco 0, nerozumiem foldr ake argumenty ocakava -- countTrue [True, False, True, True] == 3 -- countTrue :: [Bool] -> Int -- countTrue = foldr (\_ acc -> acc + 1) 0 -- 3.3 -> neviem -- productOfList [1, 2, 3, 4] == 24 -- productOfList :: [Int] -> Int -- productOfList = foldr (\_acc -> acc * ) -- 3.4 -> neviem -- perfectSquares 5 == [4, 16] perfectSquares :: Int -> [Int] perfectSquares n = [x | x <- [1..n^2 -1], even x*x] -- 3.5 -> neviem -- pythagoreanTriples 10 == [(3,4,5),(4,3,5),(6,8,10),(8,6,10)] -- pythagoreanTriples :: Int -> [(Int, Int, Int)] -- 3.6 -> neviem -- sumOfDivisors 28 == 28 -- 28 je perfektné číslo (1+2+4+7+14 = 28) -- sumOfDivisors 12 == 16 -- 1+2+3+4+6 = 16 sumOfDivisors :: Integer -> Integer sumOfDivisors n = sum ([x | x <- [1..n]], n `mod` x == 0) ---------------------------------------------- -- main (to run this code: bash> ./start) main :: IO () main = do -- Level 1 -- 1.1 print (rectanglePerimeter 3 4) -- 1.2 print (isVowel 'a') -- 1.3 print (average3 3 4 3) -- 1.4 print (bmiCategory 3) -- Level 2 -- 2.1 print (rotateLeft 2 [1, 2, 3, 4, 5]) -- 2.2 print (myReverse [1, 2, 3]) -- 2.3 print (removeAll 'a' "banana") -- 2.4 print (evenSquares [1, 2, 3, 4, 5, 6]) -- 2.5 -- print (totalScore 'A' [("Adam", 10), ("Alica", 20), ("Boris", 5)]) -- 2.6 -- print (translate [('a', '4'), ('e', '3')] "ahoj svete") -- Level 3 -- 3.1 print (listOr [False, False, False]) -- 3.2 -- print (countTrue [True, False, True, True]) -- 3.3 -- print (productOfList [1, 2, 3, 4]) -- 3.4 print (perfectSquares 5) -- 3.5 print () -- 3.6 print (sumOfDivisors 12) -- lookup "trabant" [("trabant", 100), ("peugeot", 30)] = 100 -- trabant -> key ^^^^ ^^^ -- key value