Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading