-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve 'Geometry Basics: Distance between points in 2D' kata
- Loading branch information
1 parent
fc0866a
commit c6d3a93
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Distance (distance) where | ||
|
||
-- https://www.codewars.com/kata/58dced7b702b805b200000be/train/haskell | ||
|
||
import Preloaded (Point(..)) | ||
|
||
distance :: Point -> Point -> Double | ||
distance p1 p2 = sqrt ((x p1 - x p2) ^ 2 + (y p1 - y p2) ^ 2) | ||
|
||
-- #againwhatlearned: destructure Point structure: | ||
-- distance (Point x1 y1) (Point x2 y2) = ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module DistanceSpec (spec) where | ||
|
||
import Preloaded (Point(..)) | ||
import Distance (distance) | ||
import Test.Hspec | ||
import Control.Monad (unless) | ||
|
||
spec :: Spec | ||
spec = do | ||
it "example tests" $ do | ||
test Point { x = 0, y = 1 } Point { x = 0, y = 0 } 1.0 | ||
test Point { x = 0, y = 3 } Point { x = 4, y = 0 } 5 | ||
test Point { x = 1, y = -1 } Point { x = 1, y = -1 } 0 | ||
test Point { x = 0, y = 0 } Point { x = 0, y = 0 } 0 | ||
test Point { x = 0, y = 0.3 } Point { x = 0.4, y = 0 } 0.5 | ||
|
||
epsilon :: Double | ||
epsilon = 1e-6 | ||
|
||
test :: Point -> Point -> Double -> Expectation | ||
test a b 0 = do | ||
let actual = distance a b | ||
message = "distance " ++ show a ++ " " ++ show b ++ ": expected actual value " ++ show actual ++ " to approximately equal expected value 0 ( accepted absolute error: " ++ show epsilon ++ " )" | ||
unless (abs actual <= epsilon) $ expectationFailure message | ||
test a b expected = do | ||
let actual = distance a b | ||
message = "distance " ++ show a ++ " " ++ show b ++ ": expected actual value " ++ show actual ++ " to approximately equal expected value " ++ show expected ++ " ( accepted relative error: " ++ show epsilon ++ " )" | ||
unless (abs (actual - expected) <= epsilon * abs expected) $ expectationFailure message | ||
|