forked from steger/pr3-ws202627
VL 18.11
parent
57efd635e4
commit
7c28528dd0
|
|
@ -1,17 +1,14 @@
|
||||||
import Data.Bits (Bits (xor))
|
import Data.Bits (Bits (xor))
|
||||||
|
|
||||||
-- single parameter
|
-- 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
|
-- mySign x = if x < 0 then -1 else if x > 0 then 1 else 0
|
||||||
|
|
||||||
-- two parameters
|
-- two parameters
|
||||||
|
|
||||||
-- ============= pattern matching =================
|
-- ============= pattern matching =================
|
||||||
-- mySign :: Int -> Int
|
|
||||||
-- mySign 0 = 0
|
mySign 0 = 0
|
||||||
-- mySign x = if x < 0 then -1 else 1
|
mySign x = if x < 0 then -1 else 1
|
||||||
-- ============= guards =================
|
-- ============= guards =================
|
||||||
|
|
||||||
mySign x
|
mySign x
|
||||||
|
|
@ -19,14 +16,68 @@ mySign x
|
||||||
| x < 0 = -1
|
| x < 0 = -1
|
||||||
| x == 0 = 0
|
| 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 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 ===========
|
-- =========== 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 =====================
|
-- ========================== standard deviation using lambdas =====================
|
||||||
-- The standard deviation is a measure of the amount of variation or dispersion of a set of values.
|
-- 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.
|
-- 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,
|
-- - N is the number of values,
|
||||||
-- - xi represents each value,
|
-- - xi represents each value,
|
||||||
-- - μ is the mean of the values.
|
-- - μ is the mean of the values.
|
||||||
|
stddev seq =
|
||||||
stddev seq = 0
|
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 =================
|
-- ======================= order list of strings by length =================
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ 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
|
-- 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
|
-- returns a list of required operations
|
||||||
hanoi :: (Int, Char, Char, Char) -> [String]
|
hanoi :: (Int, Char, Char, Char) -> [String]
|
||||||
|
|
||||||
-- TODO: implement here
|
-- TODO: implement here
|
||||||
|
|
||||||
-- Source, Destination, Temp
|
-- Source, Destination, Temp
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue