diff --git a/haskell/03-functions/functions.hs b/haskell/03-functions/functions.hs index bdfaf34..90f6569 100644 --- a/haskell/03-functions/functions.hs +++ b/haskell/03-functions/functions.hs @@ -1,17 +1,14 @@ import Data.Bits (Bits (xor)) -- single parameter -mySquare :: (Num a) => a -> a -mySquare x = x * x - -- mySign x = if x < 0 then -1 else if x > 0 then 1 else 0 -- two parameters -- ============= pattern matching ================= --- mySign :: Int -> Int --- mySign 0 = 0 --- mySign x = if x < 0 then -1 else 1 + +mySign 0 = 0 +mySign x = if x < 0 then -1 else 1 -- ============= guards ================= mySign x @@ -19,14 +16,68 @@ mySign x | x < 0 = -1 | x == 0 = 0 --- ============= recursive algorithms ================= +-- | otherwise = 0 auch eine möglichkeit --- ============= recursive list algorithms ================= +-- ============= recursive algorithms ================= +myFactorial 0 = 1 +myFactorial n = n * myFactorial (n - 1) + +myFibonacci 0 = 0 +myFibonacci 1 = 1 +myFibonacci n = myFibonacci (n - 1) + myFibonacci (n - 2) + +-- ============= recursive list algorithms ================= Prüfungsrelevant +myReverse [] = [] +myReverse (x : xs) = myReverse xs ++ [x] + +myLength [] = 0 +myLength (x : xs) = 1 + myLength xs + +myMap :: (a -> b) -> [a] -> [b] +myMap _ [] = [] +myMap f (x : xs) = f x : myMap f xs + +myFilter :: (a -> Bool) -> [a] -> [a] +myFilter _ [] = [] +myFilter p (x : xs) = if p x then x : myFilter p xs else myFilter p xs + +-- myFilter p l = [x | x <- l, p x] als ListCoprehansion + +myZip :: [a] -> [b] -> [(a, b)] +myZip _ [] = [] +myZip [] _ = [] +myZip (x : xs) (y : ys) = (x, y) : myZip xs ys + +myDotProduct :: (Num a) => [a] -> [a] -> a +myDotProduct _ [] = 0 +myDotProduct [] _ = 0 +myDotProduct (x : xs) (y : ys) = x * y + myDotProduct xs ys -- ============= merge sort ================= +merge :: (Ord a) => [a] -> [a] -> [a] +merge [] [] = [] +merge d [] = d +merge [] d = d +merge (x : xs) (y : ys) + | x < y = x : merge xs (y : ys) + | otherwise = y : merge (x : xs) ys + +myMergeSort :: (Ord a) => [a] -> [a] +myMergeSort [] = [] +myMergeSort [x] = [x] +myMergeSort d = + let half = length d `div` 2 + left = take half d + right = drop half d + in merge (myMergeSort left) (myMergeSort right) -- =========== Filter all numbers that can be divided by 10 =========== +canBeDividedByTen x = x `mod` 10 == 0 + +-- mit Lambdas +tens = filter (\x -> x `mod` 10 == 0) [1 .. 100] + -- ========================== standard deviation using lambdas ===================== -- The standard deviation is a measure of the amount of variation or dispersion of a set of values. -- It is defined as the square root of the average of the squared differences from the mean. @@ -38,7 +89,9 @@ mySign x -- - N is the number of values, -- - xi represents each value, -- - μ is the mean of the values. - -stddev seq = 0 +stddev seq = + let n = fromIntegral (length seq) + mean = sum seq / n + in sqrt $ foldl (\acc x -> acc + (x - mean) ^ 2) 0 seq / n -- ======================= order list of strings by length ================= diff --git a/haskell/04-hanoi/hanoi.hs b/haskell/04-hanoi/hanoi.hs index 5f64857..0626767 100644 --- a/haskell/04-hanoi/hanoi.hs +++ b/haskell/04-hanoi/hanoi.hs @@ -9,9 +9,8 @@ move n src dst = "move disk " ++ show n ++ " from " ++ [src] ++ " to " ++ [dst] -- transfers a number of disks (the Int) from a source (the first Char) rod to a destination (the second Char) rod via a temp (the third char) rod -- returns a list of required operations -hanoi :: (Int,Char,Char,Char) -> [String] - ---TODO: implement here +hanoi :: (Int, Char, Char, Char) -> [String] +-- TODO: implement here -- Source, Destination, Temp hanoiTests :: [((Int, Char, Char, Char), [String])]