将列表转换为 Prolog 中的术语 [英] Converting a list into a term in Prolog
本文介绍了将列表转换为 Prolog 中的术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
将 Prolog 列表转换为 Prolog 术语(即不是列表)的最佳方法是什么?效率,并尽可能使用现有的内置谓词?
What is the best way of converting a Prolog list into a Prolog term (that is not a list), in terms of efficiency, and using existing built-in predicates as much as possible?
界面和使用示例如下.
%% list_to_term(+List:list, +Functor:atom, -Term:term)
%
% Usage:
%
% ?- list_to_term([], myfunctor, Term).
% Term = myfunctor.
%
% ?- list_to_term([a, b, [c], D, 2], myfunctor, Term).
% Term = myfunctor(a, b, [c], D, 2).
即给定的列表(实际上是一个嵌套的术语)被展平为具有给定名称的术语.
I.e. the given list (which is actually a nested term) is flattened into a term with the given name.
我并不是说这样做有意义.(但如果您认为确实如此,请在您的回答中提供一个用例.)
I'm not saying that it makes sense to do this. (But if you think that it does, please provide a usecase in your answer.)
推荐答案
您需要使用 =..
运算符,如下所示:
You need to use the =..
operator, like so:
list_to_term(List, Functor, Term) :-
Term =.. [Functor | List].
这篇关于将列表转换为 Prolog 中的术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文