GnomeaD
GnomeaD

Reputation: 1

Finding neighbouring cells in haskell

I am trying to write a function that will look at the coordinates of two cells and return a boolean response if they are adjacent or not.

I am thinking that because of this:

data Coord = Coord {xCoord, yCoord :: Integer}
deriving (Ord, Eq, Show, Read)

My function needs to take two integers and then (this is the part i need help with) check that they are neighbours. Everything I have written will break, as it also allows for two non-neighbouring cells to return a true.

Please Help. =]

Upvotes: 0

Views: 471

Answers (1)

Frerich Raabe
Frerich Raabe

Reputation: 94319

I guess two coordinates are adjacent in your sense if they are not equal, and the distance in X and Y direction between them is at most one? If so, you could use

adjacent :: Coord -> Coord -> Bool
adjacent p q | p == q = False
adjacent (Coord x1 y1) (Coord x2 y2) = abs (x1 - x2) <= 1 && abs (y1 - y2) <= 1

Upvotes: 1

Related Questions