I’m going through a Haskell tutorial and am given this piece of code to do with moving a knight in chess:
import Control.Monad
type KnightPos = (Int,Int)
moveKnight :: KnightPos -> [KnightPos]
moveKnight (c,r) = do
(c',r') <- [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1)
,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)
]
guard (c' `elem` [1..8] && r' `elem` [1..8])
return (c',r')
in3 :: KnightPos -> [KnightPos]
in3 start = return start >>= moveKnight >>= moveKnight >>= moveKnight
canReachIn3 :: KnightPos -> KnightPos -> Bool
canReachIn3 start end = end `elem` in3 start
An exercise is to modify the functions so that canReachIn3 tells you what moves you can take to get to the end position if it is possible to get there.
This tutorial has basically no exercises so I’m having trouble with basic stuff like this…I was thinking of changing the return values of all 3 functions to [[KnightPos]] where 1 big list contains a list for every possible ordering of moves. That would probably then involve moveKnight having a [KnightPos] parameter instead of a KnightPos one, which would then defeat the whole point of monads right?
Any help/thoughts would be greatly appreciated, thanks.
It might help to step back from the Monad concept for a bit when thinking about this code, if you find that plain old list operations are more natural for you. So you can rewrite the example code (with a little bit of cleanup for legibility) as:
The secret sauce is in
concatMap. As you probably know already, it’s synonymous with>>=in theListmonad, but it might be more helpful right now to think of it as mapping a function of typeKnightPos -> [KnightPos]over a list[KnightPos]to yield a list of lists[[KnightPos]], and then flattening the result back into a single list.Okay, so now that we’ve dispensed with monads for the moment, let’s look back at the puzzle… Let’s say your initial
KnightPosis(4,4), and you want to track all possible sequences of moves from that position. So define another type synonym:Then you’d want
moveKnightto operate on these sequences, finding all possible moves from the last position in the sequence:So starting from a sequence
[(4,4)], we’d get the list of sequences:[[(4,4), (6,3)], [(4,4), (6,5)], [(4,4), (2,3)], ... ]. Then I think the only change you’d need to make toin3is to fix its type signature accordingly:I don’t think the actual implementation changes. Finally, you’ll want
canReachIn3to look something like:I’m leaving the implementation detail out here on purpose since I don’t want to ruin the puzzle for you entirely, but I’m hoping I’ve illustrated the point here that there isn’t anything particularly “special” about a list, or a list of lists, or whatever. All we’ve really done is substituted a function of type
KnightPos -> [KnightPos]with a new function of typeSequence -> [Sequence]— pretty much the same shape. So fill in the implementations of each function using whatever style feels natural, and then once you have it working, go back to the monadic style, replacingconcatMapwith>>=and so on.