Hannan
Hannan

Reputation: 47

The results my Prolog program outputs are not what what they are supposed to be

I am making a prolog program in which a person can find the marks of students. They can do this by entering this code marks_chemistry(personsname).

However when this code is entered the output is no instead of what their marks are supposed to be.

I am fairly new to prolog and coding in general so any help would be greatly appreciated.

male(albert). male(paul). female(jessica). female(rebecca). female(natalie).

marks_chemistry(paul) :-
   marksA is 85,
   write('Paul got', marksA, '85/100').

Upvotes: 1

Views: 122

Answers (1)

Some notes on your current approach:

  • Predicates always output yes/no (or they go into an infinite loop). They cannot "return" other values. However, during their evaluation, they can have side effects like printing messages to the console.

  • Prolog is case-sensitive, lower-case identifiers are understood as predicates/function symbols, upper-case identifiers as variables. Therefore, asking Prolog ?- a = b. will give you the answer no, but ?- X = b. will give you the answer X = b, asking ?- a(X) = b. will give you no but ?- a(X) = b(c). will give you X = c.

    When you write marksA is 85, Prolog will check if the term markA is equal to the arithmetic evaluation of 85, which is always false. Instead, you should use MarksA is 85. Here, MarkA is understood as a variable, and because the variable hasn't been assigned yet, Prolog will assign it with 85.

  • The write predicate doesn't support variadic arguments. You either have to use write for each argument individually, or use formatting (see here).

Based on these observations, your program could be rewritten to:

marks_chemistry(paul) :-
   MarksA is 85,
   write('Paul got '),
   write(MarksA),
   writeln('/100').

Querying the program gives:

?- marks_chemistry(paul).
Paul got 85/100
true.

Upvotes: 2

Related Questions