[Solved] prolog change show answer to print into list


When describing a list, always consider using DCGs. In your case, you can very easily obtain what you want with a few simple modifications to your code:

show_result(Squares,MaxRow,MaxCol, List) :-
    phrase(show_result(Squares,MaxRow,MaxCol,1), List).

show_result(_,MaxRow,_,Row) --> { Row > MaxRow }, !.
show_result(Squares,MaxRow,MaxCol,Row) -->
    { phrase(show_result(Squares,MaxRow,MaxCol,Row,1), Line) } ,
    [Line],
    { Row1 is Row+1 },
    show_result(Squares,MaxRow,MaxCol,Row1).

show_result(_,_,MaxCol,_,Col) --> { Col > MaxCol }, !. 
show_result(Squares,MaxRow,MaxCol,Row,Col) -->
    ( { memberchk(sq(Row,Col,X),Squares) } ->  
        [X]
    ;   [#]
    ),
    { Col1 is Col+1 },
    show_result(Squares,MaxRow,MaxCol,Row,Col1).

Example query and its result:

?- show_result([sq(1,2,'c'),sq(2,1,'A'),sq(2,2,'a'),sq(2,3,'C'),sq(3,2,'t')], 3, 3, List).
List = [[#, c, #], ['A', a, 'C'], [#, t, #]]

0

solved prolog change show answer to print into list