0
votes

I'm working on a predicate to generate a list of values from rules. I have these rules:

casilla(1,4,1,1,3).
casilla(1,4,1,2,1).
casilla(1,4,1,3,2).
casilla(1,4,1,4,4).
casilla(1,4,2,1,0).
casilla(1,4,2,2,4).
casilla(1,4,2,3,0).
casilla(1,4,2,4,0).
casilla(1,4,3,1,1).
casilla(1,4,3,2,0).
casilla(1,4,3,3,0).
casilla(1,4,3,4,2).
casilla(1,4,4,1,4).
casilla(1,4,4,2,2).
casilla(1,4,4,3,3).
casilla(1,4,4,4,1).

and I need to save the last value of each rule in a list, for example:

BOARD = [3, 1, 2, 4, 0, 4, 0, 0, 1, 0, 0, 2, 4, 2, 3, 1].

I try to recursively save every element of this form:

createBoard2(N, M, Difficulty, [VALUE]) :-
   casilla(Difficulty,N,_,_,VALUE).
createBoard2(N, M, Difficulty, [VALUE,T]) :-
   casilla(Difficulty,N,_,_,X),
   createBoard2(N, M, Difficulty,T).

but really I do not understand the error,I only returns the first VALUE thanks.

2
[VALUE,T] should be [VALUE|T]. - Paulo Moura

2 Answers

0
votes

To avoid findall/3 (why?), we need something like this inefficient, procedural code:

createBoard2(N, M, Difficulty, L) :- createBoard2(N, M, Difficulty, [], L).
createBoard2(N, M, Difficulty, Seen, [VALUE|R]) :-
   casilla(Difficulty,N,X,Y,VALUE),
   \+ memberchk(casilla(Difficulty,N,X,Y,VALUE), Seen),
   !, createBoard2(N, M, Difficulty, [casilla(Difficulty,N,X,Y,VALUE)|Seen], R).
createBoard2(_N, _M, _Difficulty, _Acc, []).
0
votes

If you don't want to use something like findall/3, then you should not store your data in the form of facts.

Alternative 1: put your casilla/5 terms into a separate data file, then use open/3 and read/2 to read these terms one by one from this file into a list.

Alternative 2: put all the casilla/5 terms into a single fact as a list, i.e.

casillas([
    casilla(1,4,1,1,3),
    casilla(1,4,1,2,1),
    ...
]).