more shit

This commit is contained in:
nora 2021-04-11 21:26:36 +02:00
parent 457d94eda4
commit 40a4b61733

View file

@ -5,6 +5,8 @@ module Lib
) )
where where
import Data.List
someFunc :: IO () someFunc :: IO ()
someFunc = putStrLn "someFunc" someFunc = putStrLn "someFunc"
@ -22,7 +24,6 @@ doubleList :: [Int] -> [Int]
doubleList [] = [] doubleList [] = []
doubleList xs = mapList (* 2) xs doubleList xs = mapList (* 2) xs
-- | Replaces all occurrences of a char in a string with another char -- | Replaces all occurrences of a char in a string with another char
replace :: String -> Char -> Char -> String replace :: String -> Char -> Char -> String
replace [] _ _ = [] replace [] _ _ = []
@ -38,7 +39,7 @@ replaceMultiple [] _ _ = []
replaceMultiple [x] c r replaceMultiple [x] c r
| x `elem` c = [r] | x `elem` c = [r]
| otherwise = [x] | otherwise = [x]
replaceMultiple (x:xs) c r replaceMultiple (x : xs) c r
| x `elem` c = r : replaceMultiple xs c r | x `elem` c = r : replaceMultiple xs c r
| otherwise = x : replaceMultiple xs c r | otherwise = x : replaceMultiple xs c r
@ -55,8 +56,7 @@ getFactors x y
| x `div` 2 < y = [x] | x `div` 2 < y = [x]
| x `mod` y == 0 = y : getFactors x (y + 1) | x `mod` y == 0 = y : getFactors x (y + 1)
| otherwise = getFactors x (y + 1) | otherwise = getFactors x (y + 1)
-- Get all prime factors for a number -- Get all prime factors for a number
primeFactor :: Int -> [Int] primeFactor :: Int -> [Int]
primeFactor n = getPrimeFactors n 2 primeFactor n = getPrimeFactors n 2
@ -66,4 +66,57 @@ getPrimeFactors :: Int -> Int -> [Int]
getPrimeFactors n m getPrimeFactors n m
| n `div` 2 < m = [n] | n `div` 2 < m = [n]
| n `mod` m == 0 = m : getPrimeFactors (n `div` m) 2 | n `mod` m == 0 = m : getPrimeFactors (n `div` m) 2
| otherwise = getPrimeFactors n (m + 1) | 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