Ad
Code
Diff
  • 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 _ [] = undefined
    • closestPoint point ps = minimumBy (\x y -> compare (dist point x) (dist point y)) ps
    • closestPoint point ps = minimumBy (compare `on` dist point) ps
Code
Diff
  • 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) where
    • dist1 = dist point p1
    • fn (dist1, p1) p2 | dist1 < dist2 = (dist1, p1) | otherwise = (dist2, p2) where
    • dist2 = dist point p2
    • closestPoint point ps = minimumBy (\x y -> compare (dist point x) (dist point y)) ps
Code
Diff
  • 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 [] = undefined
    • closestPoint point (p:ps) = closest (dist point p) p ps where
    • closest :: Float -> Point -> [Point] -> Point
    • closest dist1 p1 [] = p1
    • closest dist1 p1 (p2:ps) | dist1 < dist2 = closest dist1 p1 ps
    • | otherwise = closest dist2 p2 ps
    • where 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