Reputation: 355
I want to create a predicate that only accepts a specific input from the user and it will keep asking for the right input if the user gives the wrong input. I've created this, but its not completed because it does not ask for the new input if its wrong:
askchar(X):- write('give char'),nl, get_char(X), test(X).
test(X):- X=a, write('ok'). %accepts a
test(X):- X='1', write('ok'). %accepts 1
test(X):- write('wrong input. try again'),nl.
Upvotes: 1
Views: 2998
Reputation: 14873
Here is what I got:
askChar(Char) :- get_char(Char), test(Char), write('This is the right char, thank you.'), !.
askChar(Char) :- write('That is wrong char!'), askChar(Char).
test(s).
It asks again and again until the char s
is typed in.
Upvotes: 1
Reputation: 60014
in systems lacking a decent tail recursive optimization, processing side effects can be conveniently done with a failure driven loop
1 ?- [user].
|: askchar(C) :- repeat, get(C), (C = 0'a ; C = 0'1, ! ; fail).
% user://1 compiled 0.07 sec, 2 clauses
true.
2 ?- askchar(X).
|: 5
|: a
X = 97 .
Upvotes: 2