1
0
Fork 0

Compare commits

..

3 Commits

3 changed files with 53 additions and 23 deletions

View File

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

View File

@ -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
-- returns a list of required operations
hanoi :: (Int,Char,Char,Char) -> [String]
--TODO: implement here
hanoi (n, src, dst, tmp)
| 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
hanoiTests :: [((Int, Char, Char, Char), [String])]

View File

@ -3,30 +3,49 @@
{-# HLINT ignore "Use void" #-}
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
-- show :: (Show a, Num a, Eq a) => Complex a -> String
-- TODO
instance (Show a, Num a, Eq a, Ord a) => Show (Complex a) where
show (Complex re im)
| 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
-- (+) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a
-- (*) :: (Num a, Floating a) => Complex a -> Complex a -> Complex a
-- TODO
-- 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 (Num a, Floating a, Eq a) => Num (Complex a) where
(Complex re1 im1) + (Complex re2 im2) = Complex (re1 + re2) (im1 + im2)
(Complex re1 im1) - (Complex re2 im2) = Complex (re1 - re2) (im1 - im2)
(Complex re1 im1) * (Complex re2 im2) = Complex (re1 * re2 - im1 * im2) (re1 * im2 + im1 * re2)
instance (Fractional a, Floating a) => Fractional (Complex a) where
-- fromRational r = TODO
-- recip (Complex re im) = TODO
-- (Complex re1 im1) / (Complex re2 im2) = TODO
abs (Complex re im) = Complex (sqrt (re * re + im * im)) 0
signum (Complex re im)
| re == 0 && im == 0 = Complex 0 0
| otherwise = let mag = sqrt (re * re + im * im) in Complex (re / mag) (im / mag)
-- conj :: Num a => Complex a -> Complex a
-- TODO
fromInteger n = Complex (fromInteger n) 0
negate (Complex re im) = Complex (negate re) (negate im)
instance (Fractional a, Floating a, Eq a) => Fractional (Complex a) where
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 =