问题描述
我正在测试我的新版本 SWI prolog 并不断遇到错误:单一变量.
I was testing my new version of SWI prolog and keep coming across the error :singleton variable.
示例:
member(X,[X|T]).
member(X,[X|T]) :- member(X,T).
查找列表的成员,例如:
finds the member of a list such as :
member(yolands,[yolanda,tim])
X = yes
但是我得到了一个 singleton variables X 和 T 的错误
but instead I get a singleton variables error for X and T
如果我执行以下操作:
member(X,[X|_]).
member(X,[_|T]) :- member(X,T).
它有效但看起来很丑!
谁能解释为什么不允许使用单个变量以及是否符合 ANSI 标准?
Can anyone explain why single variables ar enot allowed and if this ANSI standard?
推荐答案
Singleton 变量在 Prolog 中没用,很容易通过编辑拼写错误引入.
Singleton variables are useless in Prolog, and are easily introduced by editing typos.
警告对我来说是受欢迎的,因为它可以很容易地发现如此频繁的错误原因.
The warning is welcome to me, as it allows to easily spot such frequent cause of error.
作为警告,您可以运行包含单例的代码,但是这些最终假定的任何值都将丢失.
Being a warning, you can run code containing singletons, but any value these eventually will assume will be lost.
我不认为 ISO 标准(从未听说过 ANSI)禁止此类变量.
I don't think that ISO standard (never heard about ANSI) forbids such variables.
你可以用这种方式重写你的例子
You could rewrite your example in this way
member(X, [Y|T]) :- X = Y ; member(X, T).
然后忘记单身.
这篇关于序言中的单例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!