开发者

warning in prolog

开发者 https://www.devze.com 2023-02-05 19:02 出处:网络
I wrote this predicate in prolog : list([]). list([X|L]) :- list(L). it works well, but I got开发者_StackOverflow this warning :

I wrote this predicate in prolog :

list([]).
list([X|L]) :- list(L).

it works well, but I got开发者_StackOverflow this warning :

    **Warning: /Users/hw6.pl:2:  
           Singleton variables: [X]** % 

what I can do to avoid it ?


The warning tells you that you have a variable used only once in that clause of the predicate list (in this case the second clause).

Why does it warns you of this ? Because it is more than often that you have misspelled the variable name. The resulting code when you misspell a variable is also a valid prolog program, so debugging would be painful if it does not warn you.

If you are not going to use that variable (X), you can use an anonymous variable instead. To use an anonymous variable you have to use _ as the term instead of a variable name.

In your example it would be:

list([]).
list([_|L]) :- list(L).


Gusbro is exactly right. When you use a variable only once you will get a singleton variable. Your program is still syntactically correct, but prolog assumes you made a mistake typing your code. The underscore variable will always unify as true if it is given any answer.

0

精彩评论

暂无评论...
验证码 换一张
取 消