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).
repeat
  • 18,496
  • 4
  • 54
  • 166
Pro
  • 173
  • 4
  • 15
  • Try `write('Wrong guess')` (use single quotes to print the message, not double quotes, which is a character array). – lurker Nov 01 '15 at 03: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. – Enigmativity Nov 01 '15 at 03: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. – lurker Nov 01 '15 at 12:31
  • @Pro can you please explain, *this is not working for me*? – lurker Nov 01 '15 at 12:32

2 Answers2

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.
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
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.

?-
lurker
  • 56,987
  • 9
  • 69
  • 103
  • ad 1: No, you will not get an array! For details on the Prolog flag `double_quotes` read http://stackoverflow.com/a/8269897/4609915 , and read the Prolog manual of your choice on `set_prolog_flag/2`. – repeat Nov 01 '15 at 17:28
  • @repeat sorry, I usually run GNU Prolog and the default behavior is the character code list. – lurker Nov 01 '15 at 17:37
  • 1
    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? – repeat Nov 01 '15 at 17:46