If you want to check if a point (x,y)
is on the same diagonal as a point (x',y')
in Haskell, a good place to start is with a type.
isDiagonal :: (Int, Int) -> (Int, Int) -> Bool
Whilst technically optional, it's good practice to give every top level function an explicit type.
Now, we need a body for this function.
isDiagonal (x, y) (x', y') = ...
If they're on the same diagonal, then x
and x'
must be as far apart from each other as y
and y'
are. So we can just check if the differences are equal. i.e
abs (x - x') == abs (y - y')
Putting it all together, we (hopefully) arrive at the desired function
isDiagonal :: (Int, Int) -> (Int, Int) -> Bool
isDiagonal (x, y) (x', y') = abs (x - x') == abs (y - y')
There's no need for &&
here, at all. But we could use it, if for example we wanted to impose an extra condition, like the new points must be on the 4x5 board (assuming 1-indexing)
isDiagonal :: (Int, Int) -> (Int, Int) -> Bool
isDiagonal (x, y) (x', y') = x' >= 1 && x' <= 4 &&
y' >= 1 && y' <= 5 &&
abs (x - x') == abs (y - y')