module ClosestPoint (closestPoint) where import Preloaded (Point,dist) import Data.List (minimumBy) import Data.Function (on) -- extracts from a list of points the closest to a given point closestPoint :: Point -> [Point] -> Point closestPoint point ps = minimumBy (compare `on` dist point) ps
- module ClosestPoint (closestPoint) where
- import Preloaded (Point,dist)
- import Data.List (minimumBy)
- import Data.Function (on)
- -- extracts from a list of points the closest to a given point
- closestPoint :: Point -> [Point] -> Point
closestPoint _ [] = undefinedclosestPoint point ps = minimumBy (\x y -> compare (dist point x) (dist point y)) ps- closestPoint point ps = minimumBy (compare `on` dist point) ps
module ClosestPoint (closestPoint) where import Preloaded (Point,dist) import Data.List (minimumBy) -- extracts from a list of points the closest to a given point closestPoint :: Point -> [Point] -> Point closestPoint _ [] = undefined closestPoint point ps = minimumBy (\x y -> compare (dist point x) (dist point y)) ps
- module ClosestPoint (closestPoint) where
- import Preloaded (Point,dist)
- import Data.List (minimumBy)
- -- extracts from a list of points the closest to a given point
- closestPoint :: Point -> [Point] -> Point
- closestPoint _ [] = undefined
closestPoint point (p1 : ps) = snd (foldl fn (dist1, p1) ps) wheredist1 = dist point p1fn (dist1, p1) p2 | dist1 < dist2 = (dist1, p1) | otherwise = (dist2, p2) wheredist2 = dist point p2- closestPoint point ps = minimumBy (\x y -> compare (dist point x) (dist point y)) ps
module ClosestPoint (closestPoint) where import Preloaded (Point,dist) -- extracts from a list of points the closest to a given point closestPoint :: Point -> [Point] -> Point closestPoint _ [] = undefined closestPoint point (p1 : ps) = snd (foldl fn (dist1, p1) ps) where dist1 = dist point p1 fn (dist1, p1) p2 | dist1 < dist2 = (dist1, p1) | otherwise = (dist2, p2) where dist2 = dist point p2
- module ClosestPoint (closestPoint) where
- import Preloaded (Point,dist)
- -- extracts from a list of points the closest to a given point
- closestPoint :: Point -> [Point] -> Point
closestPoint point [] = undefinedclosestPoint point (p:ps) = closest (dist point p) p ps whereclosest :: Float -> Point -> [Point] -> Pointclosest dist1 p1 [] = p1closest dist1 p1 (p2:ps) | dist1 < dist2 = closest dist1 p1 ps| otherwise = closest dist2 p2 pswhere dist2 = dist point p2- closestPoint _ [] = undefined
- closestPoint point (p1 : ps) = snd (foldl fn (dist1, p1) ps) where
- dist1 = dist point p1
- fn (dist1, p1) p2 | dist1 < dist2 = (dist1, p1) | otherwise = (dist2, p2) where
- dist2 = dist point p2
module Codewars.GetClosestPoint where
type Point = (Float, Float)
dist :: Point -> Point -> Float
dist (x1, x2) (y1, y2)
= sqrt (dx ^ 2 + dy ^ 2)
where (dx, dy) = (y1 - x1, y2 - x2)
closestPoint :: Point -> [Point] -> Point
closestPoint point [p] = p
closestPoint point (p : ps)
= closestBetweenTwo point p (dist point p) (closestPoint point ps)
where
closestBetweenTwo :: Point -> Point -> Float -> Point -> Point
closestBetweenTwo p0 p1 initialDist p2
| initialDist < dist point p2 = p1
| otherwise = p2
closestPoint' :: Point -> [Point] -> Point
closestPoint' point [] = point
closestPoint' point (p : ps)
= closestBetweenTwo point p (dist point p) ps
where
closestBetweenTwo :: Point -> Point -> Float -> [Point] -> Point
closestBetweenTwo p0 p1 initialDist [] = p1
closestBetweenTwo p0 p1 initialDist (p2 : ps)
| initialDist < dis = closestBetweenTwo p0 p1 initialDist ps
| otherwise = closestBetweenTwo p0 p2 dis ps
where dis = dist p0 p2
module Codewars.GetClosestPointSpec where
import Codewars.GetClosestPoint (closestPoint, closestPoint')
import Test.Hspec
import Test.QuickCheck
spec :: Spec
spec = do
describe "Points" $ do
it "finds the closest Point" $ do
let points = [(38, 19), (833, 938), (83, 49), (83, 83), (72, 15)]
closestPoint (0, 0) points `shouldBe` (38, 19)
closestPoint' (0, 0) points `shouldBe` (38, 19)
-- the following line is optional for 8.2
-- main = hspec spec