How to return to "repetition" in Prolog?

Is it possible to return to repetition in Prolog without calling a function (predicate) and without creating a new function?

I have the following code

test :- nl,
write('Welcome.'),nl,
repeat, write('Print this message again? (yes/no)'),nl,
read(Ans),nl,
(
    Ans == yes -> write('You selected yes.'), nl
;
    write('You selected no.')
).

The current output that I get is

Welcome.
Print this message again? (yes/no)
yes.
You selected yes.
true.

The end of the program.

The output I want

Welcome.
Print this message again? (yes/no)
yes.
You selected yes.
Print this message again? (yes/no)
no.

The program ends.

An easy way to output that I want to avoid (I do not want this output. I do not want it to show Welcome several times):

Welcome.
Print this message again? (yes/no)
yes.

Welcome.
You selected yes.
Print this message again? (yes/no)
no.

The program ends.

+4
source share
2 answers

Repeat

repeat/0 just defined as :

repeat.
repeat :- repeat.

Or, equivalently:

repeat :- true ; repeat.

To repeat, you need to return to the call repeatwithout explicitly executing either with failor through another unsuccessful predicate (see the example in the link above).

...
repeat,
...,
fail.

, ( ) ! , t23 > . , - repeat .

NB: !/0 .

, (btw, writeln):

test :- 
  nl,
  writeln('Welcome.'),
  repeat, 
  writeln('Print this message again? (yes/no)'),
  read(Ans),nl,
  (Ans == yes -> 
    writeln('You selected yes.'), 
    fail % backtrack to repeat
  ; writeln('You selected no.'),
    ! % cut, we won't backtrack to repeat anymore
  ).

, OP , . , ( ) , ( ) .

, , read_string(end_of_line,_,S), . read/1 Ctrl+D, .

, ->:

test :- 
  nl,
  writeln("Welcome."),
  repeat, 
  writeln("Print this message again? (yes/no)"),
  read_string(end_of_line,_,Ans),
  nl,
  write("You selected "),
  write(Ans),
  writeln("."),
  Ans == "no", % Otherwise, repeat
  !.

-> , , . : , , repeat, yes, no , , . yes : , , yes? , , . , repeat, Ans == no.

, , , :

test :- 
  nl,
  writeln("Welcome."),
  repeat, 
  writeln("Print this message again? (yes/no)"),
  read_string(end_of_line,_,Ans),
  nl,
  (memberchk(Ans,["yes","no"]) ->
    write("You selected "),
    write(Ans),
    writeln("."),
    Ans == "no",
    !
  ; writeln("Bad input" : Ans),
    fail).
+4

:

test :- 
     nl, write('Welcome.'), 
     nl, test_internal.

test_internal :- 
     write('Print this message again? (yes/no)'), nl,
     read(Ans), nl,
     (    Ans == yes 
     ->   write('You selected yes.'), nl, test_internal
     ;    Ans == no, write('You selected no.'), !
     ;    test_internal
     ).

, ( coredump one's) :

test :- 
     nl, write('Welcome.'), 
     repeat, nl,
     write('Print this message again? (yes/no)'), nl,
     read(Ans), nl,
     (    Ans == yes 
     ->   write('You selected yes.'), fail
     ;    Ans == no, write('You selected no.'), !
     ;    fail
     ).

: if-then

(->)/2 (if-then-ELSE-FAIL) (cf SWI-Prolog ). , if-then-else.

test :- 
     nl, write('Welcome.'), 
     repeat, nl,
     write('Print this message again? (yes/no)'), nl,
     read(Ans), nl,
     (    Ans == yes -> write('You selected yes.'), fail
     ;    Ans == no  -> write('You selected no.'),  !
     ).

, if-then-ELSE-FAIL --- . , (maybe, i_dont_know, i_m_afraid, i_gotta_go) .

+2

Source: https://habr.com/ru/post/1584571/


All Articles