forked from steger/pr3-ws202627
Hausaufgaben bis 18.11
parent
026466b01e
commit
57efd635e4
|
|
@ -1,2 +1,2 @@
|
|||
main :: IO ()
|
||||
main = putStrLn "Hello, World!"
|
||||
main = putStrLn "Hello, World!"
|
||||
|
|
@ -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]
|
||||
|
||||
|
||||
|
|
@ -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 ()
|
||||
|
|
|
|||
|
|
@ -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 =================
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue