1

I did this guessing program, If the user enters 10, then the program should print (write, you have guessed the number).

Else it shoudl print wrong and reads another number, and it won't quit until the user enters the right number ( what a silly program lol).

So far that's what I did, But I don't why this is not working for me.

guess_num(10):- write("You have guessed right"), nl. guess_num(X) :- X =\= 10, write("Wrong guess"), nl , read(X), guess_num(X). 
4
  • Try write('Wrong guess') (use single quotes to print the message, not double quotes, which is a character array). Commented Nov 1, 2015 at 3:05
  • @lurker - I use strawberry prolog which requires " for strings and doesn't compile if you use '. There are too many versions of prolog that do it differently. Commented Nov 1, 2015 at 3:34
  • @Enigmativity, OK. However the OP hasn't said what prolog they're using, and single quotes is the standard syntax. If I replace the quotes, the program behaves as expected. Commented Nov 1, 2015 at 12:31
  • @Pro can you please explain, this is not working for me? Commented Nov 1, 2015 at 12:32

2 Answers 2

1

This worked for me.

?- guess. guess :- read(X), check_answer(X). check_answer(10):- write("You have guessed right"), nl. check_answer(X) :- X =\= 10, write("Wrong guess"), nl, guess. 

This works better without the need for (forced) recursion:

guess:- repeat, read(X), check_answer(X), !. check_answer(10) :- write("You have guessed right"), nl. check_answer(X) :- X =\= 10, write("Wrong guess"), nl, fail. 
Sign up to request clarification or add additional context in comments.

5 Comments

@false - No, but that would work fine 10+0 doesn't unify to 10 so it's not a correct guess.
But you would not write Wrong guess
@false - Why wouldn't you? Are you saying because 10 + 0 equals 10?
Exactly. So for this case you write neither the first nor the second message.
@false - Oh, I see. It's not unifying to check_answer(X). That's annoying. I thought if X were a variable it would have unified against any input.
1

Using the mechanism repeat that @Enigmativity showed in his answer is probably the better way to implement this, rather than recursion. However, your recursive method would basically work except for a particular issue (and one suggestion):

  1. You are trying to re-instantiate a variable in your second clause, which Prolog will not allow. So if the user queries, guess_num(9). you attempt to read(X) when X = 9, which will fail unless the user enters 9 again. You need to use a new variable.

  2. Using a cut (!) in this kind of method will eliminate the choice point that will occur if the user guesses correctly. Without the cut, once the user is told they have the right answer, Prolog will prompt the user for more solutions.

Making the above corrections would give you:

guess_num(10) :- !, write("You have guessed right"), nl. guess_num(X) :- X =\= 10, write("Wrong guess"), nl, read(X1), guess_num(X1). 

Using the above code:

?- guess_num(9). Wrong guess |: 8. Wrong guess |: 10. You have guessed right true. ?- 

Without the cut:

?- guess_num(9). Wrong guess |: 8. Wrong guess |: 10. You have guessed right true ; false. ?- 

3 Comments

ad 1: No, you will not get an array! For details on the Prolog flag double_quotes read stackoverflow.com/a/8269897/4609915 , and read the Prolog manual of your choice on set_prolog_flag/2.
@repeat sorry, I usually run GNU Prolog and the default behavior is the character code list.
No harm done... Different defaults are evil; I wish we could reach some reasonable consensus on SO. I know this wiĺl not happen by tomorrow, but it still would be worth it, don't you agree?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.