forked from steger/pr3-sose2026
Compare commits
3 Commits
522fd6df17
...
5dff437064
| Author | SHA1 | Date |
|---|---|---|
|
|
5dff437064 | |
|
|
ad48ab3983 | |
|
|
d08714a7a9 |
|
|
@ -13,7 +13,14 @@ 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"
|
-- we are looking for the UPPERCASE names of the two oldest persons whose names ends with an a sorted alphabetically"
|
||||||
|
|
||||||
extractedNames = []
|
extractedNames =
|
||||||
|
map (map toUpper)
|
||||||
|
. sort
|
||||||
|
. map fst
|
||||||
|
. take 2
|
||||||
|
. sortOn (negate . snd)
|
||||||
|
. filter (\(name, _) -> last name == 'a')
|
||||||
|
$ zip names ages
|
||||||
|
|
||||||
test1 = Test.HUnit.TestCase (assertEqual "ExtractedNames" ["EMMA", "ISABELLA"] extractedNames)
|
test1 = Test.HUnit.TestCase (assertEqual "ExtractedNames" ["EMMA", "ISABELLA"] extractedNames)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,12 @@ 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]
|
||||||
|
hanoi (n, src, dst, tmp)
|
||||||
--TODO: implement here
|
| n <= 0 = []
|
||||||
|
| n == 1 = [move 1 src dst]
|
||||||
|
| otherwise = hanoi (n - 1, src, tmp, dst)
|
||||||
|
++ [move n src dst]
|
||||||
|
++ hanoi (n - 1, tmp, dst, src)
|
||||||
|
|
||||||
-- Source, Destination, Temp
|
-- Source, Destination, Temp
|
||||||
hanoiTests :: [((Int, Char, Char, Char), [String])]
|
hanoiTests :: [((Int, Char, Char, Char), [String])]
|
||||||
|
|
|
||||||
|
|
@ -3,30 +3,49 @@
|
||||||
{-# HLINT ignore "Use void" #-}
|
{-# HLINT ignore "Use void" #-}
|
||||||
import Test.HUnit
|
import Test.HUnit
|
||||||
|
|
||||||
data Complex a = TODO
|
data Complex a = Complex a a deriving (Eq)
|
||||||
|
|
||||||
instance (Show a, Num a, Eq a) => Show (Complex a) where
|
instance (Show a, Num a, Eq a, Ord a) => Show (Complex a) where
|
||||||
-- show :: (Show a, Num a, Eq a) => Complex a -> String
|
show (Complex re im)
|
||||||
-- TODO
|
| re == 0 && im == 0 = "0"
|
||||||
|
| re == 0 && im == 1 = "i"
|
||||||
|
| re == 0 && im == -1 = "-i"
|
||||||
|
| re == 0 = show im ++ "i"
|
||||||
|
| im == 0 = show re
|
||||||
|
| im == 1 = show re ++ "+i"
|
||||||
|
| im == -1 = show re ++ "-i"
|
||||||
|
| im > 0 = show re ++ "+" ++ show im ++ "i"
|
||||||
|
| otherwise = show re ++ show im ++ "i"
|
||||||
|
|
||||||
instance (Num a, Floating a) => Num (Complex a) where
|
instance (Num a, Floating a, Eq a) => Num (Complex a) where
|
||||||
-- (+) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a
|
(Complex re1 im1) + (Complex re2 im2) = Complex (re1 + re2) (im1 + im2)
|
||||||
-- (*) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a
|
(Complex re1 im1) - (Complex re2 im2) = Complex (re1 - re2) (im1 - im2)
|
||||||
-- TODO
|
(Complex re1 im1) * (Complex re2 im2) = Complex (re1 * re2 - im1 * im2) (re1 * im2 + im1 * re2)
|
||||||
-- abs (Complex re im) = TODO
|
|
||||||
-- signum (Complex re im) = TODO
|
|
||||||
-- fromInteger n = TODO
|
|
||||||
-- negate :: (Num a, Floating a) => Complex a -> Complex a
|
|
||||||
-- negate (Complex re im) = Complex (negate re) (negate im)
|
|
||||||
|
|
||||||
instance (Fractional a, Floating a) => Fractional (Complex a) where
|
abs (Complex re im) = Complex (sqrt (re * re + im * im)) 0
|
||||||
-- fromRational r = TODO
|
signum (Complex re im)
|
||||||
-- recip (Complex re im) = TODO
|
| re == 0 && im == 0 = Complex 0 0
|
||||||
|
| otherwise = let mag = sqrt (re * re + im * im) in Complex (re / mag) (im / mag)
|
||||||
|
|
||||||
-- (Complex re1 im1) / (Complex re2 im2) = TODO
|
fromInteger n = Complex (fromInteger n) 0
|
||||||
|
negate (Complex re im) = Complex (negate re) (negate im)
|
||||||
|
|
||||||
-- conj :: Num a => Complex a -> Complex a
|
instance (Fractional a, Floating a, Eq a) => Fractional (Complex a) where
|
||||||
-- TODO
|
fromRational r = Complex (fromRational r) 0
|
||||||
|
recip (Complex re im) =
|
||||||
|
let denom = re * re + im * im
|
||||||
|
in Complex (re / denom) (negate im / denom)
|
||||||
|
|
||||||
|
(Complex re1 im1) / (Complex re2 im2) =
|
||||||
|
let denom = re2 * re2 + im2 * im2
|
||||||
|
in Complex ((re1 * re2 + im1 * im2) / denom) ((im1 * re2 - re1 * im2) / denom)
|
||||||
|
|
||||||
|
conj :: (Num a) => Complex a -> Complex a
|
||||||
|
conj (Complex re im) = Complex re (negate im)
|
||||||
|
|
||||||
|
-- Imaginary unit
|
||||||
|
i :: (Num a) => Complex a
|
||||||
|
i = Complex 0 1
|
||||||
|
|
||||||
tests :: Test
|
tests :: Test
|
||||||
tests =
|
tests =
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue