From 604228b57eb939389afb78e38e3bef7d61db3a24 Mon Sep 17 00:00:00 2001 From: ChaosLL1337 Date: Wed, 25 Nov 2026 09:41:52 +0000 Subject: [PATCH] Vorlesung 25.11 --- haskell/03-functions/functions.hs | 15 ++++++++++++++ haskell/04-hanoi/hanoi.hs | 4 +++- haskell/05-geometry/geometry.hs | 34 ++++++++++++++++++++++++++++--- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/haskell/03-functions/functions.hs b/haskell/03-functions/functions.hs index 90f6569..0a98969 100644 --- a/haskell/03-functions/functions.hs +++ b/haskell/03-functions/functions.hs @@ -1,4 +1,6 @@ import Data.Bits (Bits (xor)) +import Data.List (sort, sortBy) +import Data.Ord qualified -- single parameter -- mySign x = if x < 0 then -1 else if x > 0 then 1 else 0 @@ -71,6 +73,16 @@ myMergeSort d = right = drop half d in merge (myMergeSort left) (myMergeSort right) +--- andere Sort Algorithmen machen für Haskell Übung + +insertionsort :: (Ord a) => [a] -> [a] +insertionsort [] = [] +insertionsort (x : xs) = inserter (insertionsort xs) x + +inserter :: (Ord a) => [a] -> a -> [a] +inserter [] x = [x] +inserter (x : xs) y = if y < x then y : x : xs else x : (inserter xs y) + -- =========== Filter all numbers that can be divided by 10 =========== canBeDividedByTen x = x `mod` 10 == 0 @@ -95,3 +107,6 @@ stddev seq = in sqrt $ foldl (\acc x -> acc + (x - mean) ^ 2) 0 seq / n -- ======================= order list of strings by length ================= +-- sortByLength d = Data.List.sortBy (\a b -> compare (length a) (length b)) d +-- sortByLength d = Data.List.sortBy (Data.Ord.comparing length) d +-- sortByLength d = reverse $ Data.List.sortBy (Data.Ord.comparing length) d \ No newline at end of file diff --git a/haskell/04-hanoi/hanoi.hs b/haskell/04-hanoi/hanoi.hs index 0626767..0fa1760 100644 --- a/haskell/04-hanoi/hanoi.hs +++ b/haskell/04-hanoi/hanoi.hs @@ -10,7 +10,9 @@ 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 (n, src, dst, tmp) + | n > 0 = hanoi (n - 1, src, tmp, dst) ++ [move n src dst] ++ hanoi (n - 1, tmp, dst, src) + | otherwise = [] -- Source, Destination, Temp hanoiTests :: [((Int, Char, Char, Char), [String])] diff --git a/haskell/05-geometry/geometry.hs b/haskell/05-geometry/geometry.hs index ad56b4c..daac630 100644 --- a/haskell/05-geometry/geometry.hs +++ b/haskell/05-geometry/geometry.hs @@ -1,5 +1,33 @@ --- data Entity = +data Shape a = Circle {radius :: a} | Rectangle {height :: a, width :: a} deriving (Show) --- data Shape = +area :: (Floating a) => Shape a -> a +area (Rectangle h w) = h * w +area (Circle r) = r ^ 2 * pi --- area :: Shape a -> a \ No newline at end of file +circumference :: (Floating a) => Shape a -> a +circumference (Circle r) = 2 * pi * r +circumference (Rectangle h w) = 2 * h + 2 * w + +scale :: (Floating a) => a -> Shape a -> Shape a +scale f (Circle r) = Circle (r * f) +scale f s = (scaleWidth f . scaleHeight f) s + +scaleWidth :: (Num a) => a -> Shape a -> Shape a +scaleWidth f s = s {width = width s * f} + +scaleHeight :: (Num a) => a -> Shape a -> Shape a +scaleHeight f s = s {height = height s * f} + +instance (Show a) => Show (Shape a) where + show (Circle r) = "Circle with radius " ++ show r + show (Rectangle h w) = "Rectangel with height " ++ show h ++ " and with " ++ show w + +class Meassurable s where + area2 :: (Floating a) => s a -> a + +instance Meassurable Shape where + area2 (Rectangle h w) = h * w + area2 (Circle r) = r ^ 2 * pi + +messurementSummary :: (Meassurable a, show a) => a -> String +messurementSummary m = "The area of " ++ "is " area2 m \ No newline at end of file