Skip to content

Instantly share code, notes, and snippets.

@nandor
Last active October 20, 2015 18:59
Show Gist options
  • Select an option

  • Save nandor/359c06d7dab5c6d73dd8 to your computer and use it in GitHub Desktop.

Select an option

Save nandor/359c06d7dab5c6d73dd8 to your computer and use it in GitHub Desktop.
repeatM :: Monad m => m a -> (a -> m a) -> Int -> m a
repeatM m _ 0
= m
repeatM m f n
= repeatM (m >>= f) f (n - 1)
inRange :: (Int, Int) -> Bool
inRange (x, y)
= x `elem` [0..9] && y `elem` [0..9]
knight :: (Int, Int) -> Int -> [(Int, Int)]
knight start n
= repeatM [start] move n
where
possibleMoves
= [ (-1, -2), (-1, 2)
, ( 1, -2), ( 1, 2)
, (-2, 1), (-2, -1)
, ( 2, 1), ( 2, -1)
]
move (x,y)
= [(x + dx, y + dy) | (dx, dy) <- possibleMoves, inRange (x + dx, y + dy)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment