Prolog递归始终以no结束

时间:2014-12-13 12:37:48

标签: recursion prolog

我有这个代码,我想在控制台中打印一块板子。代码在打印方面正如预期的那样工作,但最终这样做会返回“否”,而不是“是”。如何使此递归返回true?

主要代码:

printLine(_,Size,Size).
printLine([H1,H2|R],Size,Indice) :- 
        write(H1), write(' '), write(H2), write('   '),
        IndiceNovo is Indice+1,
        printLine(R,Size,IndiceNovo).

printTops([]).
printTops([H|T]) :-   write(H),write('    '),printTops(T).

printBoard(_,_,Size,Size*2,_):- nl,nl, write('end').
printBoard(Numeros,Topos,Size,Indice,BarrelIndex):- 
        calculateDiv(Size,D), nl,
        writeDiv(D), nl,

        TopoIndex1 is (1+BarrelIndex*Size),
        TopoIndex2 is (Size + BarrelIndex*Size),

        slice(Topos,TopoIndex1,TopoIndex2,TopsTemp), 
        write(' '), printTops(TopsTemp),nl, 

        Indice2 is Indice+1,

        Index1 is (1+Indice*Size*2), Index2 is (Size*2 + (Indice*Size*2)),
        slice(Numeros,Index1,Index2,Linha), 
        printLine(Linha,Size,0),nl,
        Index21 is (1+(Indice2*Size*2)), Index22 is (Size*2 + (Indice2*Size*2)),
        slice(Numeros,Index21,Index22,Linha2), 
        printLine(Linha2,Size,0),nl,

        IndiceNovo is Indice+2, NovoBarrelIndex is BarrelIndex+1,
        printBoard(Numeros,Topos,Size,IndiceNovo,NovoBarrelIndex).

printBoard(Board,Topos):-
        length(Topos,Size),
        Size1 is truncate(sqrt(Size)),
        printBoard(Board,Topos,Size1,0,0).

测试代码:

 p:-
        B= [1,2,3,4,5,6,7,8,3,4,1,5,7,8,2,6,2,1,4,3,6,7,8,5,5,7,6,8,1,3,4,2,4,6,8,7,3,2,5,1,7,8,2,1,4,5,6,3,6,3,5,2,8,4,1,7,8,5,7,6,2,1,3,4],
        T= [10,13,26,23,15,21,17,19,25,18,14,90,22,20,64,84],
        printBoard(B,T).

我的输出:

enter image description here

1 个答案:

答案 0 :(得分:1)

你需要用is / 2评估算术。

printBoard(_,_,Size,Size2,_):- Size2 is Size*2, nl,nl, write('end').