Last active
August 29, 2015 13:55
-
-
Save ramirez7/8722411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Coord = (Int, Int) | |
| type Direction = Coord -> Coord | |
| type Boundary = Coord -> Bool | |
| north :: Direction | |
| north (row, col) = (row - 1, col) | |
| south :: Direction | |
| south (row, col) = (row + 1, col) | |
| east :: Direction | |
| east (row, col) = (row, col + 1) | |
| west :: Direction | |
| west (row, col) = (row, col - 1) | |
| isInBounds :: Boundary | |
| isInBounds (row, col) = and [row >= 0, row < 8, col >= 0, col < 8] | |
| -- Starting Coord is not included in the walk | |
| walkDirection :: Boundary -> Direction -> Coord -> [Coord] | |
| walkDirection bound dir crd = takeWhile bound $ tail $ iterate dir crd | |
| -- Some sample REPL exchanges: | |
| -- walkDirection isInBounds north (3,3) | |
| -- => [(2,3), (1,3), (0,3)] | |
| -- Directions compose nicely: | |
| -- walkDirection isInBounds (north . west) (3,3) | |
| -- => [(2,2), (1,1), (0,0)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment