# Haskell: Finding if a coordinate is a neighbour from an Integer

Page 1 of 1

## 3 Replies - 2735 Views - Last Post: 28 March 2012 - 05:31 PM

Reputation: 0
• Posts: 3
• Joined: 20-March 12

# Haskell: Finding if a coordinate is a neighbour from an Integer

Posted 28 March 2012 - 03:30 AM

Hi there,

I am trying to write a piece of code that will allow me to get a boolean response as to weather two coordinates are adjacent.

Coordinates "Coord" have been defied as integers. This is from a prior function tht takes x coord and y coord and returns an integer.

I understand what needs to be done here. The function will need to check that the integers correlate to adjacent coordinates. My problem is trying to find a way to express that.

What I have so far is this: (not sure if it is correct or not.

``` isNeighbouringCoord :: Coord -> Coord -> Bool
isNeighbouringCoord m n
| m == n = False
```

Any help will be greatly appreciated. Thanks =]

Is This A Good Question/Topic? 0

## Replies To: Haskell: Finding if a coordinate is a neighbour from an Integer

### #2 ishkabible

• spelling expret

Reputation: 1702
• Posts: 5,861
• Joined: 03-August 09

## Re: Haskell: Finding if a coordinate is a neighbour from an Integer

Posted 28 March 2012 - 03:46 PM

If something is adjacent then both the X, and Y values of each must be no more than 1 away. You can check if a number is one away by checking to see if adding 1 or subtracting 1 from one of 'm's values equals the corresponding value in 'm'.

This post has been edited by ishkabible: 28 March 2012 - 03:48 PM

Reputation: 0
• Posts: 3
• Joined: 20-March 12

## Re: Haskell: Finding if a coordinate is a neighbour from an Integer

Posted 28 March 2012 - 05:02 PM

ishkabible, on 28 March 2012 - 03:46 PM, said:

If something is adjacent then both the X, and Y values of each must be no more than 1 away. You can check if a number is one away by checking to see if adding 1 or subtracting 1 from one of 'm's values equals the corresponding value in 'm'.

I unerstn that this would work for those valuses dircly eft, right above and below the cell, but what about those diagonally adjacent to the cell?

### #4 ishkabible

• spelling expret

Reputation: 1702
• Posts: 5,861
• Joined: 03-August 09

## Re: Haskell: Finding if a coordinate is a neighbour from an Integer

Posted 28 March 2012 - 05:31 PM

it still works becuase along the X access they are only 1 apart and along the Y access they are only 1 apart; so it would still be considered adjacent.

edit:
if you where looking for a more mathematical solution you could use the distance and just make sure it wasn't greater than the square root of 2.

This post has been edited by ishkabible: 28 March 2012 - 05:32 PM