Knight's tour: Difference between revisions
→{{header|Haskell}}: Slight disaggregation, pruned out some surplus magic from a list.
(→{{header|Haskell}}: Slight disaggregation, pruned out some surplus magic from a list.) |
|||
Line 3,202:
=={{header|Haskell}}==
<lang Haskell>
import Data.List (minimumBy, (\\), intercalate, sort)
import Data.Ord (comparing)
import Data.Char (ord, chr)
Line 3,216 ⟶ 3,218:
newSquare = minimumBy (comparing (length . findMoves)) possibilities
possibilities = findMoves $ head moves
findMoves = (\\ moves) .
knightMoves >>=
(\(i, j) ->
let a = x + i
b = y + j
in bool [] [(a, b)] (onBoard a && onBoard b))
knightMoves :: [(Int, Int)]
knightMoves =
let deltas = (<$> [1, 2]) =<< [id, negate]
in deltas >>=
(\i -> deltas >>= (bool [] . return . (i, )) <*> ((abs i /=) . abs))
onBoard :: Int -> Bool
|