LeetCode 1232. Check If It Is a Straight Line in F#
URL
Check If It Is a Straight Line - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/1232/main.fsx
let checkStraightLine (coordinates: (int * int) list) : bool =
let diff (x1, y1) (x2, y2) = x2 - x1, y2 - y1
let x1, y1 = List.head coordinates
let x2, y2 = List.item 1 coordinates
let xDiff, yDiff = diff (x1, y1) (x2, y2)
if xDiff = 0 then
List.forall (fun (x, _) -> x = x1) coordinates
else
coordinates
|> List.windowed 2
|> List.forall (fun lst ->
match lst with
| (a1, b1) :: (a2, b2) :: [] -> a2 - a1 = xDiff && b2 - b1 = yDiff
| _ -> failwith "never reach here")