Knight's tour: Difference between revisions
→{{header|Haskell}}: Tidied.
(→{{header|Haskell}}: Tidied.) |
|||
Line 4,086:
=={{header|Haskell}}==
<lang Haskell>
import Data.List (intercalate, minimumBy, sort, (\\)
import Data.Ord (comparing)
▲import Data.Char (ord, chr)
---------------------- KNIGHT'S TOUR ---------------------
type Square = (Int, Int)
Line 4,100:
| otherwise = knightTour $ newSquare : moves
where
newSquare =
minimumBy
(comparing (length . findMoves))
possibilities
possibilities = findMoves $ head moves
findMoves = (\\ moves) . knightOptions
Line 4,106 ⟶ 4,109:
knightOptions :: Square -> [Square]
knightOptions (x, y) =
knightMoves >>= go . bimap (+ x) (+ y)
where
go
| onBoard a && onBoard b =
| otherwise = []
knightMoves :: [(Int, Int)]
knightMoves = (deltas >>=) . go =<< deltas
where
go i x
| abs i /= abs x = [(i, x)]
| otherwise = []
onBoard :: Int -> Bool
onBoard = (&&) . (0 <) <*> (9 >)
-
startPoint :: String
startPoint = "e5"
Line 4,130 ⟶ 4,136:
main =
printTour $
algebraic
algebraic <$> knightTour [(\[x, y] -> (ord x - 96, ord y - 48)) startPoint]▼
<$> knightTour
where
printTour [] = return ()
|