diff --git a/src/Lib.hs b/src/Lib.hs index 2c3ecd6..23c520e 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -5,6 +5,8 @@ module Lib ) where +import Data.List + someFunc :: IO () someFunc = putStrLn "someFunc" @@ -22,7 +24,6 @@ doubleList :: [Int] -> [Int] doubleList [] = [] doubleList xs = mapList (* 2) xs - -- | Replaces all occurrences of a char in a string with another char replace :: String -> Char -> Char -> String replace [] _ _ = [] @@ -38,7 +39,7 @@ replaceMultiple [] _ _ = [] replaceMultiple [x] c r | x `elem` c = [r] | otherwise = [x] -replaceMultiple (x:xs) c r +replaceMultiple (x : xs) c r | x `elem` c = r : replaceMultiple xs c r | otherwise = x : replaceMultiple xs c r @@ -55,8 +56,7 @@ getFactors x y | x `div` 2 < y = [x] | x `mod` y == 0 = y : getFactors x (y + 1) | otherwise = getFactors x (y + 1) - - + -- Get all prime factors for a number primeFactor :: Int -> [Int] primeFactor n = getPrimeFactors n 2 @@ -66,4 +66,57 @@ getPrimeFactors :: Int -> Int -> [Int] getPrimeFactors n m | n `div` 2 < m = [n] | n `mod` m == 0 = m : getPrimeFactors (n `div` m) 2 - | otherwise = getPrimeFactors n (m + 1) \ No newline at end of file + | otherwise = getPrimeFactors n (m + 1) + +checksum :: Int -> Int +checksum 0 = 0 +checksum n = (n `mod` 10) + checksum (n `div` 10) + +sumList :: [Int] -> Int +sumList [] = 0 +sumList [x] = x +sumList (x : xs) = x + sumList xs + +sumListF :: [Int] -> Int +sumListF = foldl (+) 0 + +{- +'sumListF xs = foldl (\a n -> a + n) 0 xs' can be eta reduced to 'sumListF = foldl (\a n -> a + n) 0' +why? due to currying, 'foldl (\a n -> a + n) 0' returns a function that is then applied to xs +sp instead of applying a function to a value that just applies another function to that value, we can just let that +outer function be the inner function directly + +the lambda expression '(\a n -> a + n)' can also be changed to (+) +why? the lambda is a function that takes two inputs and adds them, exactly the same as (+) + +this can be further reduced to sum but like no +-} + +fizzBuzz :: IO () +fizzBuzz = putStrLn $ intercalate "\n" [fizzBuzzSingle n | n <- [1..100]] + +fizzBuzzSingle :: Int -> String +fizzBuzzSingle n + | n `mod` 15 == 0 = "FizzBuzz" + | n `mod` 5 == 0 = "Buzz" + | n `mod` 3 == 0 = "Fizz" + | otherwise = show n + + + +-- connect a string list so that it overlaps the strings +-- ["hi", "india", "ares", "resolution"] -> "hindiaresolution" +connectStrings :: [String] -> String +connectStrings [] = [] +connectStrings [x] = x +connectStrings (x : y : []) = "" +connectStrings _ = "not implemented yet" + +connectStrings2 :: String -> String -> String +connectStrings2 x y = [] + +getCommonPart :: String -> String -> String +getCommonPart [] _ = [] +getCommonPart x y + | x `isPrefixOf` y = x + | otherwise = getCommonPart (tail x) y