-3

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.

false
  • 10,264
  • 13
  • 101
  • 209
SnDnFn
  • 1
  • 1

1 Answers1

1

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 :)

9000
  • 39,899
  • 9
  • 66
  • 104
  • thanks 9000. my focus is on the "immutable" knightProblem = kSelector . kFilter . kGenerator chain equation. I only "solved" the problem by introducing compound datatypes so that every component function has only one argument, like kGenerator :: Job -> ..., but that's a different problem and not the specified one :) – SnDnFn Apr 12 '16 at 22:22
  • If you need, you can pass an argument "through" a function without changing it. For instance, `kFilter` can accept a tuple `(Position, [Moves])` and return `(Position, [Moves])`, just passing the position to `kSelector` which needs it. – 9000 Apr 13 '16 at 02:47