Knight's tour: Difference between revisions
→{{header|Haskell}}: Applied hlint and hindent – slightly simplified the type of the input, and slightly uncluttered the main :: IO() function
No edit summary |
(→{{header|Haskell}}: Applied hlint and hindent – slightly simplified the type of the input, and slightly uncluttered the main :: IO() function) |
||
Line 3,031:
=={{header|Haskell}}==
<lang Haskell>import Data.Char (ord, chr)
import Data.List (minimumBy, (\\), intercalate, sort)▼
import Data.Ord (comparing)
▲import Data.List (minimumBy, (\\), intercalate, sort)
type Square = (Int, Int)
board :: [Square]
board =
[ (x, y)
| x <- [1 .. 8]
, y <- [1 .. 8] ]
knightMoves :: Square -> [Square]
knightMoves (x, y) = filter (
where
[ (x + i, y + j)
| i <- jv
, j <- jv
, abs i /= abs j ]
jv = [1, -1, 2, -2]
knightTour :: [Square] -> [Square]
knightTour moves
|
where
newSquare = minimumBy (comparing
candMoves
findMoves = (\\ moves) . knightMoves
main = do▼
printTour $ map toAlg $ knightTour [sq]▼
where toAlg (x,y) = [chr (x + 96), chr (y + 48)]▼
▲ toSq [x,y] = ((ord x) - 96, (ord y) - 48)
printTour [] = return ()▼
printTour tour = do▼
putStrLn $ intercalate " -> " $ take 8 tour▼
printTour $ drop 8 tour▼
toAlg :: (Int, Int) -> String
e5 -> f7 -> h8 -> g6 -> h4 -> g2 -> e1 -> f3▼
-- TEST -----------------------------------------------------------------------
sq :: (Int, Int)
sq = toSq "e5" -- Input: starting position on the bord, e.g. (5, 5) as "e5"
where
{{Out}}
▲<pre>e5 -> f7 -> h8 -> g6 -> h4 -> g2 -> e1 -> f3
g1 -> h3 -> g5 -> h7 -> f8 -> d7 -> b8 -> a6
b4 -> a2 -> c1 -> d3 -> b2 -> a4 -> b6 -> a8
Line 3,076 ⟶ 3,087:
a5 -> c6 -> a7 -> c8 -> d6 -> b5 -> d4 -> e2
c3 -> d1 -> f2 -> h1 -> g3 -> e4 -> f6 -> g8
h6 -> g4 -> h2 -> f1 -> e3 -> f5 -> e7 -> d5</pre>
=={{header|Icon}} and {{header|Unicon}}==
|