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.ListLANGUAGE (minimumBy,TupleSections (\\), intercalate, sort)#-}
 
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) . knightMovesknightOptions
 
knightMovesknightOptions :: Square -> [Square]
knightMovesknightOptions (x, y) =
knightMoves >>=
[(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)] >>=
(\(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
9,659

edits