Search code examples
prologlogicdeclarative

What does this error in Prolog mean?


I'm making a little program in Prolog to find out the capitals of a country entered, bu I get this error all the time on line 10 and 11:

ERROR: c:/users/cesar downs/documents/capital.pl:10: Syntax error: Operator expected ERROR: c:/users/cesar downs/documents/capital.pl:11: Syntax error: Operator expected

I really don´t get it, heres the code:

capital_of(guatemala, guatemala).
capital_of(tegucigalpa, honduras).
capital_of(san_salvador, el_salvador).
capital_of(managua, nicaragua).
capital_of(san_jose, costa_rica).
capital_of(panamá, panamá).

%Rules

Capital:- write(‘Enter the country: ‘),Read(Country),Answer(Country).
Answer(Country):- capital_of(Country,City),Write(‘The capital of: ‘),Write(Country),Write(‘ is ‘),Write(City).

Solution

  • As an aside, also consider using format/2. For example, instead of:

    answer(Country) :-
        capital_of(Capital, Country),
        write('The capital of: '),
        write(Country),
        write(' is '),
        write(Capital).
    

    you can write:

    answer(Country) :-
        capital_of(Capital, Country),
        format("The capital of: ~w is ~w", [Country,Capital]).