Knight's tour: Difference between revisions
Content added Content deleted
Line 4,090: | Line 4,090: | ||
import Data.List (intercalate, minimumBy, sort, (\\)) |
import Data.List (intercalate, minimumBy, sort, (\\)) |
||
import Data.Ord (comparing) |
import Data.Ord (comparing) |
||
import Control.Monad (join) |
|||
---------------------- KNIGHT'S TOUR --------------------- |
---------------------- KNIGHT'S TOUR --------------------- |
||
Line 4,111: | Line 4,112: | ||
knightMoves >>= go . bimap (+ x) (+ y) |
knightMoves >>= go . bimap (+ x) (+ y) |
||
where |
where |
||
go |
go move |
||
| |
| uncurry (&&) (both onBoard move) = [move] |
||
| otherwise = [] |
| otherwise = [] |
||
knightMoves :: [(Int, Int)] |
knightMoves :: [(Int, Int)] |
||
knightMoves = |
knightMoves = |
||
((>>=) <*> (\deltas n -> deltas >>= go n)) |
((>>=) <*> (\deltas n -> deltas >>= go n)) [1, 2, -1, -2] |
||
[1, 2, -1, -2] |
|||
where |
where |
||
go i x |
go i x |
||
Line 4,126: | Line 4,126: | ||
onBoard :: Int -> Bool |
onBoard :: Int -> Bool |
||
onBoard = (&&) . (0 <) <*> (9 >) |
onBoard = (&&) . (0 <) <*> (9 >) |
||
both :: (a -> b) -> (a, a) -> (b, b) |
|||
both = join bimap |
|||
--------------------------- TEST ------------------------- |
--------------------------- TEST ------------------------- |