Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/prolog/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 从列表列表中获取列表_List_Prolog - Fatal编程技术网

List 从列表列表中获取列表

List 从列表列表中获取列表,list,prolog,List,Prolog,嗨,我是prolog的新手,搞不清楚我在做什么。 我的任务似乎相当简单。我想从列表中检索一个列表 我还想检索列表列表中每个列表的Nieme(第n个)元素来构建一个列表 第一个任务示例: ListList([[1,2],[3,4],[5,6]]) ListList([[1,2],[3,4],[5,6]], N) %Where N is the index i want ex:2 仅返回[1,2] 第二个任务示例: ListList([[1,2],[3,4],[5,6]]) ListList

嗨,我是prolog的新手,搞不清楚我在做什么。 我的任务似乎相当简单。我想从列表中检索一个列表

我还想检索列表列表中每个列表的Nieme(第n个)元素来构建一个列表

第一个任务示例:

ListList([[1,2],[3,4],[5,6]])
ListList([[1,2],[3,4],[5,6]], N)  %Where N is the index i want ex:2
仅返回[1,2]

第二个任务示例:

ListList([[1,2],[3,4],[5,6]])
ListList([[1,2],[3,4],[5,6]], N)  %Where N is the index i want ex:2
仅返回[2,4,6]

谢谢

编辑:到目前为止我得到了什么:


还在浏览网页。我发现nth1适用于我的第一个任务。还有一些类似:

match([Elem|_],Num,Num,Elem) :-
    !.
match([_|Tail],Num,Count,MatchedNumber) :-
    Count < Num,
    Count1 is Count+1,
    match(Tail,Num,Count1,MatchedNumber).
匹配([Elem | |,]Num,Num,Elem):-
!.
匹配([[u124; Tail],Num,Count,MatchedNumber):-
计数

到目前为止,第二个任务仍然没有任何结果。我需要使用双重递归,让我们来解决第一个问题。您只需要给定列表的标题。这就是全部。列表可以写成
[H | T]
,其中
H
是头(或第一个元素),而
T
是尾(列表的结果,本身就是列表)

如果您有一个列表,
[a,b,c]
,并将其与
[H | T]
统一起来,Prolog将做一些有趣的事情。在Prolog提示符下尝试以下操作:

?- [a,b,c] = [H|T].
观察你得到了什么。还请注意,您可以在谓词的开头进行统一

还有一件事要尝试。进入Prolog并输入以下内容:

?- [user].
head_tail(L, Head, Tail) :-
     L = [Head | Tail].

foo([H|T], H, T).

Ctrl^D
true.
然后尝试:

?- head_tail([a,b,c], H, T).
并尝试:

?- foo([a,b,c], H, T).
看看你能得到什么。你可以用它来理解如何解决你的第一个问题

在第二个问题中,还可以使用统一。不过,在本例中,您现在正在递归地遍历列表

match([], ...).         % Handle the empty list
match([H|T], ...) :-    % Handle the general case with recursion
    % do something here with H and/or T
    ...
    match(T, ...).

您只需找出每种情况下的
是什么样子。

谓词不会返回任何内容。它统一了一个变量…是的,很抱歉。我的意思是将它存储在我将在函数中传递的另一个参数中。类似ListList([[1,2],[3,4],[5,6],[List])(a)谓词必须以小写字母开头;(b)你尝试过什么?还在浏览网页。我发现N适合我的第一个任务。还有一些类似于:match([Elem | |,Num,Num,Elem):-!。匹配(Tail),Num,Count,MatchedNumber):-Count