Skip to content

Commit b3171fc

Browse files
committed
Solve 'Orthogonal Vectors' kata
1 parent 15172d0 commit b3171fc

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Orthogonal.hs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module Orthogonal where
2+
3+
-- https://www.codewars.com/kata/53670c5867e9f2222f000225/train/haskell
4+
5+
isOrthogonal :: [Int] -> [Int] -> Bool
6+
isOrthogonal xs ys = (== 0) $ dotProduct xs ys
7+
8+
dotProduct :: [Int] -> [Int] -> Int
9+
dotProduct [] [] = 0
10+
dotProduct (x : xs) (y : ys) = x * y + dotProduct xs ys
11+
12+
13+
14+
-- #againwhatlearned
15+
-- use `zipWith`
16+
-- dotProduct = (sum .) . zipWith (*)

test/OrthogonalSpec.hs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module OrthogonalSpec where
2+
3+
import Orthogonal (isOrthogonal)
4+
import Test.HUnit
5+
import Test.Hspec
6+
7+
runTest :: ([Int], [Int], Bool) -> Expectation
8+
runTest (xs, ys, expected) =
9+
assertEqual (unwords [show xs, show ys]) expected $
10+
isOrthogonal xs ys
11+
12+
spec :: Spec
13+
spec = do
14+
describe "Fixed Tests" $ do
15+
it "Works for some example tests" $ do
16+
mapM_
17+
runTest
18+
[ ([1, 2], [2, 1], False),
19+
([1, -2], [2, 1], True),
20+
([7, 8], [7, -6], False),
21+
([-13, -26], [-8, 4], True),
22+
([1, 2, 3], [0, -3, 2], True),
23+
([3, 4, 5], [6, 7, -8], False),
24+
([3, -4, -5], [-4, -3, 0], True),
25+
([1, -2, 3, -4], [-4, 3, 2, -1], True),
26+
([2, 4, 5, 6, 7], [-14, -12, 0, 8, 4], True),
27+
([5, 10, 1, 20, 2], [-2, -20, -1, 10, 5], False)
28+
]

0 commit comments

Comments
 (0)