Prolog type checking

2019-02-21 12:14发布

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?

5条回答
爷的心禁止访问
2楼-- · 2019-02-21 12:33

You could try this code:

isList([_|_]).
isList([]).

Hope it helps.

查看更多
Juvenile、少年°
3楼-- · 2019-02-21 12:42

To check if a variable is bound to a list, you can use is_list/1.

查看更多
成全新的幸福
4楼-- · 2019-02-21 12:48

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('[]').
查看更多
ゆ 、 Hurt°
5楼-- · 2019-02-21 12:50

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

查看更多
登录 后发表回答