程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了遍历序言列表并返回一个包含所有正值索引的列表大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决遍历序言列表并返回一个包含所有正值索引的列表?

开发过程中遇到遍历序言列表并返回一个包含所有正值索引的列表的问题如何解决?下面主要结合日常开发的经验,给出你关于遍历序言列表并返回一个包含所有正值索引的列表的解决方法建议,希望对你解决遍历序言列表并返回一个包含所有正值索引的列表有所启发或帮助;

我需要获取列表中所有正元素的每个索引,并使用所有这些索引创建一个新列表

例如

[-1,2,-5] -> [1]
[1,-5] -> [0,1]

我已经有了一个获取索引的谓词,但我不明白如何遍历每个值并在最后返回一个列表。现在我的谓词看起来像

indexOf([Element|_],Element,0) :- !.
indexOf([_|Tail],Index) :-
  indexOf(Tail,Index1),!,Index is Index1+1.

iterate([],Res) :- Res.
iterate([H|T],Res) :- 
    H>0,indexOf([H|T],H,Ind),append([],[Ind],Res),iterate(T,Res).

iterate([H|T],Res) :- 
    H=<0,Res).

但编译后,我收到此错误

**input** 
iterate([-1,-2,3],X).

**Output**

SandBox restriction!
Could not derive which predicate may be called from
      call(C)
      iterate([],A)
      iterate([3],A)
      iterate([-2,A)
      iterate([-1,A)

请告诉我,我做错了什么?以及为什么会出现这个错误

解决方法

您错误地使用了 Prolog:

iterate([],Res) :- Res.

调用绑定到 Res 的术语(希望是谓词的名称),但不会返回 Res

SWISH 不会让您执行无法确定它们是否安全的疯狂调用,因此会出现错误。

但是为什么这么复杂的代码呢?按规定做:

% ---
% gimme_positives(List,Positive)
% gimme_positives_2(Idx,List,Positive)
% ---

% We call this

gimme_positives(List,Indexes) :-
   gimme_positives_2(0,Indexes).

% This is the "helper" which additionally needs an index

gimme_positives_2(_,[],[]).                  % If List empty,we are done.

gimme_positives_2(Idx,[L|Ls],[Idx|More]) :-  % Case of L positive
   L >= 0,IdxPlus is Idx+1,gimme_positives_2(IdxPlus,Ls,More).       % recursive call

gimme_positives_2(Idx,More) :-        % Case of L negative
   L < 0,More).       % recursive call  

那么:

?- gimme_positives([],X).
X = [] ;
false.

?- gimme_positives([-1,2,-5],X).
X = [1] ;
false.

?- gimme_positives([1,X).
X = [0,1] ;
false.

这实际上是 foldl/4 的情况……一旦您对高阶谓词和组合列表感到宾至如归:

gimme_positives_foldl(List,Indexes) :-
   foldl(
      selector,% the list of integers        
      [0,Indexes],% the initial value: index 0 as first,and the RESULT list as second element 
      [_FinalIndex,[]]).  % the final value: an index we don't care about and the termination of the result list: []

selector(L,[Idx,[Idx|More]],[IdxPlus,More]) :-
   L >= 0,IdxPlus is Idx+1.

selector(L,More],More]) :-
   L < 0,IdxPlus is Idx+1.

我什至无法完全解释为什么我会这样写。

但它有效:

?- gimme_positives_foldl([],X).
X = [].

?- gimme_positives_foldl([-1,X).
X = [1] ;
false.

?- gimme_positives_foldl([1,1] ;
false
,

在 SWI-Prolog 中,您可以使用谓词 nth0/3findall/3

positive_element_indexes(List,Indexes) :-
    findall(Index,(nth0(Index,Element),Element > 0),Indexes).

一些例子:

?- positive_element_indexes([1,-2,3,-4,5],Indexes).
Indexes = [0,4].

?- positive_element_indexes([-1,3],Indexes).
Indexes = [2].

?- positive_element_indexes([-1,Indexes).
Indexes = [1,2].

?- positive_element_indexes([-1,-3],Indexes).
Indexes = [].

大佬总结

以上是大佬教程为你收集整理的遍历序言列表并返回一个包含所有正值索引的列表全部内容,希望文章能够帮你解决遍历序言列表并返回一个包含所有正值索引的列表所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: