1

I'm doing a Prolog project for uni that's based on the Kakuro game.

I'm having trouble with the predicate spaces_puzzle/2 that does the following:

  • Receives the Puzzle in the form of a list of lists that represents each file of the puzzle:
  [[[sum_vertical, sum_horizontal], P11, P12, [sum_vertical, sum_horizontal],P13, P14...],
   [[sum_vertical, sum_horizontal], P21, P22, P23, P24, ...],
   [[sum_vertical, sum_horizontal], P31, P32, P33, P34, P35 ...],
   ...]
  • and returns a list like this [space(sum_horizontal, [P11,P12]), space(sum_horizontal, [P13,P14]), space(sum_horizontal, [P21,P22,P23,P24]) ...]

I was thinking of using a maplist with a predicate that I have already defined as spaces_in_file/3 which will read a list like [[sum_vertical, sum_horizontal], P11, P12, [sum_vertical, sum_horizontal],P13, P14...] and output in the wanted formatting: [space(sum_horizontal, [P11,P12]), space(sum_horizontal, [P13,P14])...]

I don't understand the syntax for the predicate. The "Goal" I want is the spaces_in_file/3. spaces_in_file(H_V, File, Spaces) is a simple bagof where H_V is a character that says which of the sums I should use, File is the file I want to check for the spaces, and Spaces is my output.

So should it be something like this:

spaces_puzzle(Puzzle, Spaces):-
     maplist(spaces_in_file(h, File, Spaces), Puzzle, File)

I want my file to be one of the lists of the Puzzle. I can't seem to get the syntax right.

For now I just want the correct Result for the files, I can easily adapt to get the columns with the transpose/2 predicate which is suggested by the professors.

Appreciate any advice!

Will Ness
  • 70,110
  • 9
  • 98
  • 181
Antonio BC
  • 33
  • 5
  • 1
    The first argument of [`maplist/2,3...`](https://stackoverflow.com/a/6683502/772868) is not a proper goal but rather an *incomplete* goal which lacks further arguments that will be supplied element-wise by the lists of the remaining arguments. – false May 13 '21 at 11:19
  • Should my maplist be like this: maplist(spaces_file, horizontal, Puzzle, Files), ? – Antonio BC May 13 '21 at 12:49

1 Answers1

0

maplist/N takes a functor and some lists, and checks whether for each element in each list, the predicate named as that functor and with arguments from those lists, is true (you can fix some arguments by adding them to the functor), so e.g:

maplist( append, [[1], [1, 2], [1, 2, 3]], [[2, 4], [5], [6, 7]], List).

is equivalent to the conjunction

append( [1],       [2, 4], _A1 ),  List = [ _A1 | _T1 ],
append( [1, 2],    [5],    _A2 ),  _T1  = [ _A2 | _T2 ],
append( [1, 2, 3], [6, 7], _A3 ),  _T2  = [ _A3 | _T3 ],  _T3 = [].

which will return:

%%  List  =  [ _A1, _A2, _A3 ]
List = [[1, 2, 4], [1, 2, 5], [1, 2, 3, 6, 7]]. 

Your predicate spaces_in_file takes three arguments, H_V, Line and Spaces, you can do a maplist by fixing H_V and then giving Line and Spaces to maplist as arguments, i.e:

maplist( spaces_in_file(h), Lines, Spaces)
Will Ness
  • 70,110
  • 9
  • 98
  • 181