在给定索引和许多元素的列表中创建子列表。序言 [英] Create a sublist from a list given an index and a number of elements. Prolog

查看:225
本文介绍了在给定索引和许多元素的列表中创建子列表。序言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决一个简单的序言问题,但我无法解决它。从列表中需要创建一个给定索引I的子列表,然后从I给出下一个给定为N的元素。如果索引大于列表长度,我将得到子列表为空。如果N(元素的数量)大于列表中的其余元素,我将从I到所有元素直到结束。

I am trying to solve a simple prolog question but I am not able to solve it. From a list a need to create a sublist given the index I and then from I the next elements given as N. If the index is greater than the list lenght I will get the sublist empty. If N (number of elements) is greater than the rest of elements in the list I will get all the elements from I until the end.

在这里,我得到了一个部分对于赋值,我可以从索引I得到下一个元素N.现在我询问赋值中的其他部分:

Here, I got one part of the assignment, I can get from the index I, the next elements N. Now I ask about the other parts in the assignment:

1)当(索引)比列表长度长,我必须在子列表中得到一个空列表。

1) When I (index) is longer than the list length, I have to get an empty list in the sublist.

?- sublist([a,b,c,d],5,2,L)

L=[]

2)当 N (下一个元素)大于我们休息的元素数量,我需要从该位置获取所有元素直到结束。

2) When N (Next elements) is greater than the number of elements we have rest, I need to get all the elements from that position till the end.

?- sublist([a,b,c,d],4,4,L)

L=[d]      

我已经拥有的代码是下一个代码,这个代码正在运行:

The code I already have is the next one, this one is working:

sublist([X|_],1,1,[X]).
sublist([],_,_,[]).% I use this one for the case bases
sublist([X|Xs],1,K,[X|Ys]):-
       K>1, 
       K1 is K-1,
       sublist(Xs,1,K1,Ys).
sublist([_|Xs],I,K,Ys):-
       I > 1, 
       I1 is I-1,
       sublist(Xs,I1,K,Ys).


推荐答案

sublist([X|_], 1, 1, [X]).

这是一个很好的条款。它表示从列表 [X | _] 中取出的长度为1的子列表是 [X]

This is a good clause. It says that a sublist of length 1 starting at 1 taken from the list [X|_] is [X].

sublist([X|Xs], 1, K, [X|Ys]) :-
    K > 1, 
    K1 is K - 1,
    sublist(Xs, 1, K1, Ys).

这也是一个很好的条款。它说从 [X | Xs] 开始的长度 K 的子列表以<$ c $开头c> X 并且尾部 Ys 这是长度 K-1 的子列表从第一个列表( Xs )的尾部开始,从1开始。

This is also a good clause. It says that the sublist of length K starting at 1 taken from [X|Xs] starts with X and has a tail Ys which is the sublist of length K-1 from the tail of the first list (Xs) starting at 1.

sublist([_|Xs], I, K, Ys) :-
    I > 1, 
    I1 is I - 1,
    K1 is K - 1,
    sublist(Xs, I1, K1, Ys).

此子句有问题。如果您有一个列表 [_ | Xs] 并且想要获取长度 K 的子列表,请从<$ c开始$ c>我(对于大于1),你取长度 K-1 从它的尾部开始,位置 I-1 。问题是:为什么子列表现在需要长度 K-1 这个子句的目的应该是将问题减少到如果你正在处理 1 的起始索引,那么让第二个句子处理其余的事情。

This clause has an issue. If you have a list [_|Xs] and want to take a sublist of length K start at I (for I greater than 1), you take the sublist of length K-1 from its tail starting at position I-1. The question is: why would the sublist now need to be length K-1? The purpose of this clause should be to reduce the problem to the case where you're dealing with a starting index of 1, then let the second clause take care of the rest.

然后在您对所需行为的定义中,您有:如果N(元素数量)大于列表中其余元素,我将从I获取所有元素直到结束。这个概念目前不在任何条款中。基本案例是您的第一个子句,特别需要长度为1来生成长度为1的列表。您需要另一个基本案例子句来处理第一个列表为空但 K 可能仍然是任何值:

Then in your definition of the desired behavior, you have: If N (number of elements) is greater than the rest of elements in the list I will get all the elements from I until the end. This notion currently isn't in any of the clauses. The base case is currently your first clause which specifically requires a length of 1 to produce a list of length 1. You need another base case clause that handles the case where the first list goes empty but K might still be any value:

sublist([], ?, _, ?).

只需用符合逻辑的东西填写 。 :)

Just fill in the ? with something logical. :)

这篇关于在给定索引和许多元素的列表中创建子列表。序言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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