haskell Chess Knight Tour: Function composition

301 Views Asked by At

I've a hard time understanding a Chess Knight problem concerning function composition. The exercise is a generator/filter/selector chain with a given wrapper function (knightProblem) which glues everything together.

It is unclear to me how the function kGenerator as the first piece in the chain should handle multiple parameters:


-- Chess Knight Problem: Generate all Knight moves of length NrMoves
-- that end at the target position
knightProblem :: StartPos -> NrMoves -> TargetPos -> [Moves]
knightProblem =  kSelector . kFilter . kGenerator 

-- kGenerator: needs StartPos, NrMoves, generates all sequences of length NrMoves
-- kFilter: remove all moves which contain invalid positions
-- kSelector: keep all moves which terminate at TargetPos

kGenerator :: ???
???


I'm looking for hints on how to handle this kind of problems.

Best regards.

1

There are 1 best solutions below

2
9000 On

Try writing down type signatures for the other functions.

-- kSelector: keep all moves which terminate at TargetPos
-- something like
kSelector :: Position -> [Moves] -> [Moves]

-- kFilter: remove all moves which contain invalid positions
-- something like
kFilter :: [Moves] -> [Moves]

So it looks like kGenerator should provide kFilter with [Moves]:

kGenerator :: Position -> [Moves]

Think about what [Moves] are; this is likely something like [[Position]], a list of position lists representing the chain of moves.

An obvious way to generate moves from a given position would be doing the 8 possible moves, and then recursively generating more moves from each of these positions.

Hopefully this will help you get through your assignment :)