0

I am trying to use get_coeff_value() but the process never end.

get_coeff_value(Val,[],[]).
get_coeff_value(Val, [X|T],Coeff_List):- 
    get_coeff_value(Val,T,Coeff_List1),
    get_val(Val,[X], Coeff),
    insert_end(Coeff_List1, Coeff, Coeff_List).

command : get_coeff_value('H', ['H'-5, 'C'-2], Coeff).

I tried get_val() and insert_end() both end perfectly. However this one display :

command result

It gives me the answer I am waiting for but it never stops the process.

false
  • 10,264
  • 13
  • 101
  • 209
cece
  • 3
  • 3

1 Answers1

0

This looks the same as what happens if you query e.g. ?- member(X, [1,2,3]). ; your code has left choicepoints and the Prolog toplevel has found one solution but has not explored the whole search space of your code, so it is waiting for your input.

Press ? to see the help, ; or space to continue searching for more answers, or a to abort the search and stop there.

SWI Prolog help on pressing ?

  Possible actions:
  ; (n,r,space,TAB): redo              | t:         trace&redo
  *:                 show choicepoint  | c (a,RET): stop
  w:                 write             | p:         print
  b:                 break             | h (?):     help
TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87
  • Thank you, it explains a lot. Later i want to use get_coeff_value() in another function. Do you know a way to stop get_coeff_value() after giving me the first answer ? – cece Jan 16 '23 at 02:38
  • @cece you can stick `!` the [cut operator](https://www.swi-prolog.org/pldoc/doc_for?object=!/0) as the last call inside it, or wrap the call to get_coeff_value in [`once()`](https://www.swi-prolog.org/pldoc/man?predicate=once%2f1), but they can be bad habits to get into. Cutting the search tree without understanding what it does can mean valid answers exist but your code will never find them. The harder way is to spend months learning to write deterministic Prolog code, and write the code so it gives the answer and leaves no choicepoints. – TessellatingHeckler Jan 16 '23 at 13:47