Search code examples
prolog

I'm trying to do a voting system using Prolog


My code:

voterid(X):- (X>=18), write("Candidate is eligible for casting a vote").
voterid(X):- (X=<18), write("Candidate is not eligible for casting a vote").

My query:

I want to check the nationality of the voter - must be American to be eligible.

How do I do it?


Solution

  • Let's suppose X is not just only the age, but the profile of a person. It would be a predicate with some atoms, in your case an atom for the age and another one for the nationality.

    You could have:

    voterid(person(Age, american)) :- 
        Age >= 18,
        write("Candidate is eligible for casting a vote").
    

    If you want to accept people from certain nationalities, you can adapt the code as follows:

    nationality(american).
    nationality(anotherone).
    
    voterid(person(Age, Nationality)) :-
        Age >= 18,
        nationality(Nationality),
        write("Candidate is eligible for casting a vote").