For Programmers: Free Programming Magazines  


Home > Archive > Prolog > October 2004 > User-question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author User-question
Marc Wamser

2004-10-02, 3:56 pm

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


Markus Triska

2004-10-02, 3:56 pm

Marc 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.
Matthias Kretschmer

2004-10-02, 3:56 pm

Marc Wamser wrote:
> "askUser(P) :- write('Yes or no?'), (read(Answer)="yes" -> P=true; P=fail)."


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
Marc Wamser

2004-10-02, 3:56 pm

Hello 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


Markus Triska

2004-10-02, 3:56 pm

Marc 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.
Marc Wamser

2004-10-02, 8:56 pm

Thank You.

/Marc


student

2004-10-03, 8:39 pm

Marc 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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com