1
0
Fork 0

Vorlesung 25.11

main
ChaosLL1337 2026-11-25 09:41:52 +00:00
parent 7c28528dd0
commit 604228b57e
3 changed files with 49 additions and 4 deletions

View File

@ -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

View File

@ -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])]

View File

@ -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
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