You are viewing a single comment's thread. Return to all comments →
Full haskell solution calculating distance between all points in a list and summing over them
main = interact $ show . perimiter . getPairs . map (read :: String -> Float) . tail . words perimiter :: [(Float, Float)] -> Float perimiter xs = sum [distanceBetween (xs !! (n+1)) (xs !! n) | n <- [0..((length xs) - 2)]] + distanceBetween (head xs) (last xs) distanceBetween :: (Float, Float) -> (Float, Float) -> Float distanceBetween (a, b) (c, d) = sqrt $ (a-c)^2 + (b-d)^2 getPairs :: [Float] -> [(Float, Float)] getPairs (x:y:[]) = [(x, y)] getPairs (x:y:xs) = (x, y) : getPairs xs
Seems like cookies are disabled on this browser, please enable them to open this website
Compute the Perimeter of a Polygon
You are viewing a single comment's thread. Return to all comments →
Full haskell solution calculating distance between all points in a list and summing over them