0

So I have this knowledge base and I am trying to achieve the goals in the code given as comments.

%format of foo: foo(Tag,NumList) 
foo(a, [2, 4]). 
foo(b,[2, 8, 8, 6,2]). 
foo(c,[4, 8, 8, 8, 7]). 
foo(d,[7, 8, 8, 2]). 
foo(e,[5, 8, 9, 6]).
foo(f,[2, 5]). 
foo(g,[2, 6]). 
foo(h, [2, 8, 2]). 
foo(i, [2, 8, 8, 2]).
foo(j, [2, 3]).

However there is a problem with the goo part. When I only give Total_num to goo and get the unified results for Foo_list, it gives the results but after that it just gets stuck. Nothing works and I have to close the interpreter all the time.

I tried putting cuts to goo helpers but nothing works. Also when I change the order of predicates in goo(putting goo_ordered in front), it doesn't give the list and just gets stuck. How can I fix this problem? What causes it?

Thank you

%returns the sum of the numbers the NumList
foo_sum(Tag,SUM):- foo(Tag,List),foo_sum_helper(List,SUM).

foo_sum_helper([],0).
foo_sum_helper([H|T],Result):- foo_sum_helper(T,Prev_result), Result is H + Prev_result.

%foo diff find the last number in the list.
%It should remain the if it is less than or equal to four, otherwise substract 8 from it
foo_diff(Tag,Diff):- foo(Tag,List),foo_diff_helper(List,Diff).

foo_diff_helper([Last],Result):- Last =< 4, Result is Last,!.
foo_diff_helper([Last],Result):- Last > 4, Result is Last - 4,!.
foo_diff_helper([_,X|T],Result):- foo_diff_helper([X|T], Result).

%goo takes a list of foo's and a number that represents the total number of each foo_sum of foo's.
%Total of foo_diff must be 0
%Also the list of foo's must be in the ascending order.(foo_sum of the first foo in the list is the least one.)

goo(Foo_list,Total_num):- goo_sum(Foo_List,Total_num),goo_diff(Foo_list,0),goo_ordered(Foo_list).

goo_ordered([]).
goo_ordered([_]).
goo_ordered([X,Y|Z]):- foo_sum(X,NUMX),foo_sum(Y,NUMY),NUMX =< NUMY, goo_ordered([Y|Z]).

goo_sum([X],RESULT):- foo_sum(X,RESULT).
goo_sum([H|T],RESULT):- goo_sum(T,PREV_RESULT),foo_sum(H,NUMH), RESULT is NUMH + PREV_RESULT.

goo_diff([X],RESULT):- foo_diff(X,RESULT).
goo_diff([H|T],RESULT):- goo_diff(T,PREV_RESULT),foo_diff(H,HDIFF), RESULT is HDIFF + PREV_RESULT.
false
  • 10,264
  • 13
  • 101
  • 209
  • You can `trace.` your program to see on what points it backtracks and get stuck in infinite recursion. – Willem Van Onsem May 17 '19 at 19:42
  • I tried but there is a lot of checking going on and when I try the helpers separately they return one thing and just quit. goo_sum(X,20) for example. It just gives [i] and stops. However when I run goo in total I don't get it. I get the desired result but then it stucks. – Mark R. Chandar May 17 '19 at 19:58

1 Answers1

2

What causes it?

Assuming you mean the looping of goo_sum(X, 20):

For the query I get quite a lot of answers. Actually, too much for me. So I will instead consider

?- goo_sum(X, 20), false.

which loops. The A reason is the following very compact

goo_sum([X],RESULT):- false, foo_sum(X,RESULT).
goo_sum([H|T],RESULT):-
   goo_sum(T,PREV_RESULT), false,
   foo_sum(H,NUMH), RESULT is NUMH + PREV_RESULT.

You need to fix the remaining part somehow or the loop will remain.

BTW, using names as foo and goo is not a very good idea. I still do not understand your program and so do you, I presume.

I'd rather stick to smaller programs first. Also consider to use in place of the moded arithmetic via (is)/2. Here is such a suggested improvement—for a question of your colleague presumably.

And: You got some warnings by the system about a singleton variable, you need to fix that anyway. That is s/Foo_List/Foo_list/

false
  • 10,264
  • 13
  • 101
  • 209