Mandy
Mandy

Reputation: 145

Why doesn't prolog stop on cut?

I would like to get this result:

?- numberMatrixLines(1,[[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1]],X).
X = [machinePenalty(1,[1,1,1,1,1,1,1,1]),
     machinePenalty(2 [1,1,1,1,1,1,1,1]),
     machinePenalty(3,[1,1,1,1,1,1,1,1])]

I try the following code:

numberMatrixLines(X,[],ResultMatrix):-
   writeln('should end here'),
   reverse(ResultMatrix,ResultMatrixTemp),
   initWith(ResultMatrixTemp,ResultMatrix),
   !.
numberMatrixLines(X,[H|T],ResultMatrix):-
   append_in_front(machinePenalty(X,H),ResultMatrix,ResultMatrixTemp),
   writeln(ResultMatrixTemp),
   incr(X,X1),
   numberMatrixLines(X1,T,ResultMatrixTemp).            

incr(X, X1) :-
    X1 is X+1.                

append_in_front(X,[],[X]).
append_in_front(X,L1,[X|L1]).

The result is correct when numberMatrixLines(X,[],ResultMatrix) is reached. HOWEVER, the predicate won't stop there and return X , as it's supposed to.

What can be done to make it stop in that line?

Upvotes: 0

Views: 79

Answers (1)

Paulo Moura
Paulo Moura

Reputation: 18663

A straight-forward solution would be (I moved the input list to the first argument to take advantage of Prolog first-argument indexing to avoid spurious choice-points and the need of cuts):

% number_matrix_lines(+list, +integer, -list)
number_matrix_lines([], _, []).
number_matrix_lines([Line| Lines], I, [machine_penalty(I,Line)| NumberLines]) :-
    J is I + 1,
    number_matrix_lines(Lines, J, NumberLines).

Sample call:

| ?- number_matrix_lines([[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1]], 1, NumberLines).

NumberLines = [machine_penalty(1,[1,1,1,1,1,1,1,1]), machine_penalty(2,[1,1,1,1,1,1,1,1]), machine_penalty(3,[1,1,1,1,1,1,1,1])]
yes

P.S. Note that Prolog coding guidelines advise using underscores in predicate names instead of CamelCase.

Upvotes: 1

Related Questions