Code Comments
Programming Forum and web based access to our favorite programming groups.Hello,
I defined this rule:
"askUser(P) :- write('Yes or no?'), (read(Answer)="yes" -> P=true; P=fail)."
which doesn't work...
I want to ask the User. When he answers "yes" then P should be "true" so
that the rule succeeds. Otherwise it shall fail.
How can I write it correctly?
Thanks,
Marc
Post Follow-up to this messageMarc Wamser wrote:
>
> How can I write it correctly?
Be sure to check the documentation for read/1, and try something similar
to this:
askUser(P) :-
write('Yes or no?'),
read(Answer),
(Answer = "yes" -> P=true; P=fail).
Best regards,
Markus.
Post Follow-up to this messageMarc Wamser wrote:
> "askUser(P) :- write('Yes or no?'), (read(Answer)="yes" -> P=true; P=fail)."[/colo
r]
well first, predicates don't return values like functions/functional
procedures in other programming language. so your read(Answer)="yes" is
not a good way (this should always be unsuccessful - matching the record
read(Answer) to "yes" is unsuccessful -> P is matched against fail.
Don't know what implementation you use, but "normal" Prolog
implementation should behave like: read(X) reads a term from the input.
So if you want to match against the string "yes", the user has to type
"yes". If the user just inputs yes. the atom yes is unified with X. The
term has to be closed with a dot. -> 'yes.' instead of just 'yes' has to
be entered by the user.
Some words to style: You could let "askUser" fail if the user answered
with something else than yes.
--
Matthias
Post Follow-up to this messageHello Matthias,
> The term has to be closed with a dot. -> 'yes.' instead of just 'yes' has
> to be entered by the user.
thanks for your hint.
I tested the example from Markus (see posting below):
askUser(P) :-
write('Yes or no?'),
read(Answer),
(Answer = "yes" -> P=true; P=fail).
Now I tried it with "askUser(X)" and then I answered "yes." (with the dot
;) ).
But the answer is still "X = fail?"... Why?
Marc
Post Follow-up to this messageMarc Wamser wrote:
>
> I tested the example from Markus (see posting below):
>
> askUser(P) :-
> write('Yes or no?'),
> read(Answer),
> (Answer = "yes" -> P=true; P=fail).
Sorry, you have to remove the quotes from "yes" (yes). Otherwise, it is
treated a a list of latin1-characters.
Best regards,
Markus.
Post Follow-up to this messageMarc Wamser wrote:
> Hello,
>
> I defined this rule:
>
> "askUser(P) :- write('Yes or no?'), (read(Answer)="yes" -> P=true; P=fail)
."
>
> which doesn't work...
>
> I want to ask the User. When he answers "yes" then P should be "true" so
> that the rule succeeds. Otherwise it shall fail.
>
> How can I write it correctly?
>
> Thanks,
>
> Marc
>
>
Maybe it is more a question of describing the kind of situation that
you have in mind, as in,
user_said_yes_or_no(X) :-
repeat,
write('Please type in 'yes' or 'no' followed by '.' : '),
read(X),
yes_or_no(X),
!,
nl,
write('Thank you'),nl.
yes_or_no(yes).
yes_or_no(no).
--
billh
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.