Joshua Snider
Joshua Snider

Reputation: 795

String comparison program in swi-prolog always fails

I'm trying to write a program using swi-prolog that randomly asks people for their first or last name and prints "correct" or "incorrect" based on what they type. The current correct answers are "Hello" and "World" but regardless of what the user types, the output is false and correct/incorrect isn't printed.

start:-(Q=['What is your first name?','What is your last name?'],
    random_member(N,[0,1]),
    nth0(N,Q,X),
    writeln(X)),
    readln(S),
    check_answer(N,S).


check_answer(N,S):-A=['Hello','World'],
   nth0(N,A,X),
   writeln(X),
   (S=@=X)->writeln('correct'),
   not(S=@=X)->writeln('incorrect').

I later edited it to:

start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
read(S),
check_answer(N,S).


check_answer(N,S):-A=['Hello','World'],
       nth0(N,A,X),
       writeln(X),
       writeln(S),
       ((S=@=X))->writeln('correct') ; writeln('incorrect').

Upvotes: 0

Views: 549

Answers (1)

CapelliC
CapelliC

Reputation: 60014

I can spot two problems in your code.

a) readln/1 (undocumented) returns a list of items (then peek the first item or use read/1):

?- readln(X).
|: Hello.
X = ['Hello', '.'].

?- readln(X).
|: Hello
X = ['Hello'].

?- readln(X).
|: Hello .
X = ['Hello', '.'].

b) The pair of if then else operator (->) will always fail, because you omit the else branch on both, and the conditions are exclusives. Try

...
((S=@=X)->writeln('correct') ; writeln('incorrect')).

edit there are 2 problems. I wrongly suggested read/1. That read a Prolog term, and then read a variable if we write a variable, i.e. a UppercaseStartingSymbol. My fault. readln seems ok then, but change the pattern to select the first item.

Another problem, unrelated: you misplaced the closed parenthesis in -> 'statement'.

Here the code:

start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
readln(S),
check_answer(N,S).

check_answer(N,[S|_]):-A=['Hello','World'],
       nth0(N,A,X),
       writeln(X),
       writeln(S),
       ((S=@=X)->writeln('correct') ; writeln('incorrect')).

Upvotes: 1

Related Questions