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).