简体   繁体   中英

Uses of non-ground facts in Prolog?

In Prolog you can write a ground fact as:

lost(jen).

You can also write a non-ground fact as:

lost(X).

Does this makes any sense? Could you show me a practical/real example where non ground facts are used?

Thanks,

Another case, avoiding lists, is where most cases are "true" and you just want to exclude some few cases that are false. So you deliberately fail thoses cases, then let everything else pass through.

Then you can do, say...

inhabited(antarctica) :- !, fail.

% all the other continents are inhabited
inhabited(_).

Well, you can have other things in facts besides atoms, for example you can have lists. Once you've done that, you may want to know about a one-element list, and you can have

oneelement([X]).

Likewise, say you want to compare what is the last element in a list

lastelement([X],X).
lastelement([_|Remainder],X) :- lastelement(Remainder,X).

The very useful member predicate is defined as

member([X|_],X).
member([_|Remainder],X) :- member(Remainder,X).

Each of these uses a non-ground fact as its base case, by matching a special form that's more specific than just lost(X)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM