How to "return" two values in prolog

I have two functions, first one calculates how many negative elements are in list, the second one forms list with indexes of negatives elements. I need to write a function called goal_negative_positions, that will "return" two values. My version don’t work, it always returns false. How to make it correct?

negative_count([], 0):-!.
negative_count([Head|Tail], Count):-
  Head >= 0, !, 
  negative_count(Tail, Count).
negative_count([_Head|Tail], Count):-
  negative_count(Tail, TailCount), 
  Count is TailCount + 1.

negative_positions([], _, []):-!.
negative_positions([Head|Tail], CurPos, Positions):-
  NextPos is CurPos + 1, (
    Head >= 0, !, 
    negative_positions(Tail, NextPos, Positions);
    negative_positions(Tail, NextPos, TailPositions), 
    Positions = [NextPos|TailPositions]

goal_negative_positions([], [], 0).
goal_negative_positions(Start, Result, count):-
    negative_count(Start, count),
  negative_positions(Start, -1, Result).

>Solution :

Problem in last definition of goal_negative_positions. count argument should starts with capital letter:

goal_negative_positions(Start, Result, Count):-
  negative_count(Start, Count),
  negative_positions(Start, -1, Result).

In prolog arguments which starts with small letter a working like constant. In your case goal_negative_positions(Start, Result, count) will be true if in negative_count(Start, Count) Count will be equal count.

Leave a Reply