I am learning about monads in the book 'Learn You a Haskell for Great Good!' by Miran Lipovaca. The following example tries to find all the valid positions a knight in chess can reach from their current position:
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')
I am having trouble understanding how the code works. I know that for the following with Maybe:
a <- Just 3
The value in a
would be 3. But since a list has multiple elements, how does <-
work for lists? What would be the value in (c', r')
?