1

I have to write a simple expert system in Prolog for scheduling of classes. In this code part, I want that user add an instructor to system. For this, reading two input value but I am getting this error.

addinstructor() :-    
    read(id),
    read(courseid),     
    assert(instructor(id, courseid)),    
    write("added").

Query:

?- addinstructor().
5
cse102.    

Then, I am getting operator expected error. How do i fix this to work my code?

3
  • 1
    Hi, I will recommend reading How to Ask. Tell us more about what is the prupuse, the expected result , the context, the input, the output. Everything that make sense for your question. Dec 14, 2018 at 14:39
  • There is no need for the empty parentheses after addinstructor, either in the definition or in the query that calls it. I would recommend you remove them. Prolog variables MUST be capitalized; id and courseid are atoms, not variables, and this is a problem for your code. Also, assert/1 is non-standard, please use asserta/1 or assertz/1, which give you control over the order of the facts in the database. Dec 14, 2018 at 18:56
  • Thank you for your help @Daniel Lyons . Problem solved.
    – leiaorgana
    Dec 16, 2018 at 11:57

1 Answer 1

2

The predicate read/1 reads Prolog terms not raw data. Prolog terms end with a period.

So if instead of entering 5 if you enter 5. you will not get the error.

Instead of using the predicates found in Term reading and writing, e.g. read/1, you should use the predicates in Primitive character I/O for reading characters or Predicates that operate on strings for reading strings, e.g. read_string/3

To answer your next question of how do I implement this, see Prolog - Write out facts and reading a users input and then this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.