list Prolog 类型检查
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3864382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prolog type checking
提问by XBigTK13X
Is there a way to determine the type of an element within a list in Prolog? I know that variables aren't explicitly typed in Prolog, but I need to check whether an element is a number, a specific character, etc. How can this be accomplished?
有没有办法确定 Prolog 中列表中元素的类型?我知道在 Prolog 中没有明确输入变量,但我需要检查元素是否是数字、特定字符等。如何实现?
回答by Giulio Piancastelli
Prolog defines a group of built-in predicates for type testing purposes: var/1
, atom/1
, integer/1
, float/1
, atomic/1
, compound/1
, nonvar/1
, number/1
, all of them with quite a self-explanatory meaning if you know the data types of the language. For specific characters, you may exploit unification with that character, after checking that the element is not a free variable (otherwise unification is always successful).
Prolog 定义了一组用于类型测试目的的内置谓词:var/1
, atom/1
, integer/1
, float/1
, atomic/1
, compound/1
, nonvar/1
, number/1
,如果您了解该语言的数据类型,所有这些都具有不言自明的含义。对于特定字符,您可以在检查元素不是自由变量后利用与该字符的统一(否则统一总是成功的)。
回答by nhuthai08
You could try this code:
你可以试试这个代码:
isList([_|_]).
isList([]).
Hope it helps.
希望能帮助到你。
回答by Kevin
回答by Stanislav Trifan
to check list you could try:
要检查列表,您可以尝试:
listing(is_list/1, list_functor/1).
is_list(X) :-
functor(X, F, _),
list_functor(F).
list_functor('.').
list_functor('[]').