Issue
I am trying to move all new lists that I create into one super list of lists. Instead the output is formatted as a bunch of individually changed lists.
The problem likely stems from the fact I use between
to assign Iterations
and Size
, but I am not sure what else I can do.
Input
placeRedBlocks(4, X), loopReplace(X, Y), append([X], [Y], Z).
Expectation
Z = [[b, b, b, b], [r, r, r, b], [r, r, r, r], [b, r, r, r]]
Reality
X = [b, b, b, b],
Y = [r, r, r, b],
Z = [[b, b, b, b], [r, r, r, b]]
X = [b, b, b, b],
Y = [r, r, r, r],
Z = [[b, b, b, b], [r, r, r, r]]
X = [b, b, b, b],
Y = [b, r, r, r],
Z = [[b, b, b, b], [b, r, r, r]]
false
Code Explanation
This prolog code takes a list of values and replaces the designated values and returns it into another list.
placeRedBlocks(Length, List) :-
findall('b', between(1, Length, _), List).
replace([_|T], 0, X, [X|T]).
replace([H|T], I, X, [H|R]):-
I > -1,
NI is I-1,
replace(T, NI, X, R), !.
replaceX(A,_,0,_,A):- !.
replaceX(Line,Index,NumElm,Elm,NLine) :-
replace(Line,Index,Elm,BLine),
Index1 is Index+1,
NumElm1 is NumElm-1,
replaceX(BLine,Index1,NumElm1,Elm,NLine).
loopReplace(ListToReplace, NewList) :-
length(ListToReplace, Len),
TotalCount is Len-3,
between(0, TotalCount, Iterations),
between(3, Len, Size),
replaceX(ListToReplace, Iterations, Size, 'r', NewList).
In replaceX
:
Line
is the list you are trying to replace the values in. Index
is the position in the list where you start replacing values. NumElm
is the number of elements you are replacing in total. Elm
is the character or number that replaces the other values in the list. NLine
is the list where the output will go.
After messing around with
findall/3
andbagof/3
I found an answer that works for my question.Input
would get me