singleton variables in prolog

后端 未结 3 1347
甜味超标
甜味超标 2021-02-07 00:03

I was testing my new version of SWI prolog and keep coming across the error :singleton variable.

Example:

member(X,[X|T]).

member(X,[X|         


        
3条回答
  •  时光取名叫无心
    2021-02-07 00:16

    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.

    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).
    

    and then forget about the singleton.

提交回复
热议问题