将列表转换为Prolog中的术语 [英] Converting list to terms in Prolog

查看:76
本文介绍了将列表转换为Prolog中的术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须实现谓词cons(List, Term),该谓词将使用列表[Head|Tail]并将其转换为表示为next(Head, Tail)的术语.我该怎么做呢?我什至不知道从哪里开始.

I have to implement the predicate cons(List, Term) that will take a list [Head|Tail] and convert it to terms, represented as next(Head, Tail). How do I do this? I don't even know where to start.

以下是问题中给出的成功查询的示例:

Here is the example of a successful query given in the question:

cons([a,b,c],X).  /*query returns X=next(a,next(b,next(c,null))).*/

推荐答案

对列表执行大多数操作都将需要考虑两种情况:空列表以及带有标题和子列表的列表.通常,您的基本案例处理空列表,而归纳案例处理带有子列表的列表.

Doing most anything with lists will require that you consider two cases: the empty list and a list with a head and a sublist. Usually your base case is handling the empty list and your inductive case is handling the list with sublist.

首先考虑您的基本情况:

First consider your base case:

cons([], null).

现在处理您的归纳案件:

Now deal with your inductive case:

cons([X|Xs], next(X, Rest)) :- cons(Xs, Rest).

这篇关于将列表转换为Prolog中的术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆