Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Last active August 29, 2015 13:55
Show Gist options
  • Select an option

  • Save ramirez7/8722411 to your computer and use it in GitHub Desktop.

Select an option

Save ramirez7/8722411 to your computer and use it in GitHub Desktop.
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