Skip to content

Commit ad473cc

Browse files
committed
Solve 'Getting MAD' kata
1 parent 9a6ea82 commit ad473cc

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/GettingMAD.hs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module GettingMAD (gettingMAD) where
2+
3+
-- https://www.codewars.com/kata/593a061b942a27ac940000a7/train/haskell
4+
5+
import Data.List (sort)
6+
7+
gettingMAD :: [Int] -> Int
8+
gettingMAD = minimum . map diff . tuples . sort
9+
10+
tuples :: [a] -> [(a, a)]
11+
tuples [] = []
12+
tuples [_] = []
13+
tuples (x : y : xs) = (x, y) : tuples (y : xs)
14+
15+
diff :: (Num a) => (a, a) -> a
16+
diff (x, y) = abs (x - y)

test/GettingMADSpec.hs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module GettingMADSpec (spec) where
2+
3+
import GettingMAD (gettingMAD)
4+
import Test.Hspec
5+
6+
spec :: Spec
7+
spec = do
8+
it "example tests" $ do
9+
gettingMAD [-10, 0, -3, 1] `shouldBe` 1
10+
gettingMAD [0, 0, 0, 0] `shouldBe` 0
11+
gettingMAD [-570, 542] `shouldBe` 1112
12+
gettingMAD [-69, -808, 828, 57] `shouldBe` 126

0 commit comments

Comments
 (0)