From 57efd635e4ec9fb2ed4487a9261a2599d4d6a5cd Mon Sep 17 00:00:00 2001 From: ChaosLL1337 Date: Wed, 18 Nov 2026 07:54:22 +0000 Subject: [PATCH] Hausaufgaben bis 18.11 --- haskell/00-hello-world/hello-world.hs | 2 +- haskell/02-list-operations/aufgaben.md | 24 +++++++++++++++++++ haskell/02-list-operations/list-operations.hs | 22 +++++++++++++++-- haskell/03-functions/functions.hs | 15 ++++++++++-- 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 haskell/02-list-operations/aufgaben.md diff --git a/haskell/00-hello-world/hello-world.hs b/haskell/00-hello-world/hello-world.hs index ecf15d6..a1e2021 100644 --- a/haskell/00-hello-world/hello-world.hs +++ b/haskell/00-hello-world/hello-world.hs @@ -1,2 +1,2 @@ main :: IO () -main = putStrLn "Hello, World!" +main = putStrLn "Hello, World!" \ No newline at end of file diff --git a/haskell/02-list-operations/aufgaben.md b/haskell/02-list-operations/aufgaben.md new file mode 100644 index 0000000..5be7778 --- /dev/null +++ b/haskell/02-list-operations/aufgaben.md @@ -0,0 +1,24 @@ +x !! n +head $ drop n x +nthElement = curry (head . (uncurry drop)) + +Aufgabe 2 Vektormultipli (Skalarprodukt) +ghci> y = 1:8:9:7:8:14:[] +ghci> y +[1,8,9,7,8,14] +ghci> x = 9:8:25:14:63:[] +ghci> u = zip x y +ghci> w = sum$ map (\(x,y)->x*y)u +ghci> w +900 + +länge hallo ohne length +const 1 "hallo" +const 1 5 +map (const 1) str +foldr (+) 0 $ map (const 1) str + +paare von 2 natürlichen zahlen bis, die nicht gleich sind +[(a,b) | a <- [1..5], b <- [1..5], a /= b] + + diff --git a/haskell/02-list-operations/list-operations.hs b/haskell/02-list-operations/list-operations.hs index 96aef07..7de6d63 100644 --- a/haskell/02-list-operations/list-operations.hs +++ b/haskell/02-list-operations/list-operations.hs @@ -4,7 +4,7 @@ {-# HLINT ignore "Use void" #-} import Data.Char (toUpper) -import Data.List (sort, sortOn) +import Data.List (isSuffixOf, sort, sortOn) import Test.HUnit names = ["Oliver", "Emma", "Liam", "Ava", "Noah", "Sophia", "James", "Mia", "Elijah", "Isabella"] @@ -13,11 +13,29 @@ ages = [25, 30, 22, 29, 35, 28, 40, 26, 33, 31] -- we are looking for the UPPERCASE names of the two oldest persons whose names ends with an a sorted alphabetically" -extractedNames = [] +names2 = map (map toUpper) names + +kombi = zip names2 ages + +endetAufA kombi = filter (\(str, _) -> not (null str) && last str == 'A') kombi + +oldest = sortOn (negate . snd) (endetAufA kombi) + +extractedNames = sort $ map fst $ take 2 oldest test1 = Test.HUnit.TestCase (assertEqual "ExtractedNames" ["EMMA", "ISABELLA"] extractedNames) tests = TestList [test1] +istPrim :: Int -> Bool +istPrim n + | n < 2 = False + | otherwise = null [x | x <- [2 .. isqrt n], n `mod` x == 0] + where + isqrt = floor . sqrt . fromIntegral + +prime :: Int -> [Int] +prime x = [n | n <- [2 .. x], istPrim n] + main :: IO () main = runTestTT test1 >> return () diff --git a/haskell/03-functions/functions.hs b/haskell/03-functions/functions.hs index a601809..bdfaf34 100644 --- a/haskell/03-functions/functions.hs +++ b/haskell/03-functions/functions.hs @@ -1,12 +1,24 @@ +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 -- ============= guards ================= +mySign x + | x > 0 = 1 + | x < 0 = -1 + | x == 0 = 0 + -- ============= recursive algorithms ================= -- ============= recursive list algorithms ================= @@ -30,4 +42,3 @@ stddev seq = 0 -- ======================= order list of strings by length ================= -