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
List 序言:列表中不存在元素_List_Search_Prolog - Fatal编程技术网

List 序言:列表中不存在元素

List 序言:列表中不存在元素,list,search,prolog,List,Search,Prolog,我需要在列表中查找元素位置(作为内置谓词nth,但这里第一个元素有索引1)。但是,如果没有,我还需要一个类似“元素不存在”的输出。 我尝试了一个不太优雅的解决方案,为计数器分配了一个大值。 但这不是正确的解决方案 恐怕这很容易,但我找不到其他解决方案 有人能帮我吗 search(L):- write('searching for: '),read(E),find(L,E,Pos), out(L,E,Pos),!. out(E,Pos):- Pos &

我需要在列表中查找元素位置(作为内置谓词nth,但这里第一个元素有索引1)。但是,如果没有,我还需要一个类似“元素不存在”的输出。 我尝试了一个不太优雅的解决方案,为计数器分配了一个大值。
但这不是正确的解决方案
恐怕这很容易,但我找不到其他解决方案

有人能帮我吗

search(L):-
      write('searching for: '),read(E),find(L,E,Pos),
      out(L,E,Pos),!.

out(E,Pos):-
          Pos < 10000,
          write('element '),write(E),write(' is in position n. '),write(Pos),!.

out(E,Pos):-
          Pos > 10000,
          write('Element '),write(E),write(' is not present!'),!.

find([X|Xs],E,Pos):-
                 X \= E,
                 find(Xs,E,Pos1),
                 Pos is Pos1 + 1.
find([],_,10000).
find([X],X,1).
find([X|_],X,Pos):-
                Pos is 1,!.
搜索(L):-
写入(‘搜索:’)、读取(E)、查找(L、E、Pos),
出(左、东、右),!。
输出(E,Pos):-
Pos<10000,
写入('element')、写入(E)、写入('is in position n.)、写入(Pos),!。
输出(E,Pos):-
位置>10000,
write('Element')、write(E)、write('is not present!')、,!。
查找([X | Xs],E,Pos):-
X\=E,
查找(Xs、E、Pos1),
Pos是Pos1+1。
查找([],u130;,10000)。
查找([X],X,1)。
查找([X | |,]X,位置):-
Pos是1,!。

您想要的更多是这样的:

search(L):-
    write('searching for: '),read(E),
    finde(E).

finde(E) :-
    find(L,E,Pos), % find succeeds 
    out(L,E,Pos),!. % and prints, cut ensures we don't try next clause

finde(E) :- write('Element '),write(E),write(' is not present!'),!. % called if not found

这样,您就不需要一个奇怪的find子句,它总是成功地处理大量数据。让它失败。

记住,您不仅限于从find/3谓词返回数字



search(L):-
        write('searching for: '),
        read(E),
        find(L,E,Pos),
        fail.
search(_).

find(L,E,0) :-
        \+(append(_,[E|_],L)),!.
find(L,E,Pos) :-
        append(L0,[E|_],L),
        length(L0,Len0),
        Pos is Len + 1.
find(_,_,-1).

out(E,0) :-
        writef('element %t is not present. ',[E]),!.
out(E,-1) :-
        writef('element %t is no longer present. ',[E]),!.
out(E,Pos) :-
        writef('element %t is in position %t. ',[E,Pos]),!.

您可以返回任何其他原子,如下所示:

find([], _, notfound).
find([H|_], H, 1) :- !
find([_|T], H, Pos) :-
        find(T, H, Found),
        (
            integer(Found), !,
            Pos is Found+1
        );(
            Pos = notfound
        ).
然而,尽管返回值更具描述性,但这种样式在编写时有点笨拙


编辑以修复错误。

使用值0表示缺少的项如何?您的第一个元素有索引1,因此这似乎是一个安全的选项。因为在尾部递归中,prolog执行行“Pos is Pos1+1”。在每种情况下。我不建议在尾部递归中使用0,只作为返回值,这是我认为您寻求的,而不是“大值”。查看Raceimaztion的答案,了解如何返回丢失物品案例的任何内容。这个答案的代码并不是尾部递归的,但可以通过使用辅助谓词来解决。然而,如果你对z5h给出的“让它失败”的答案感到满意,那当然是优雅的。我认为你的答案非常有效!谢谢您还可以启用尾部递归,例如,使用find/4(可能失败)和find/3(在缺少项的情况下返回描述性值或零值)。我不明白。。。在我的swi prolog中,我有以下输出:15?-find([2,3,4],2,Pos)。位置=1。16?-查找([2,3,4],3,位置)。错误:is/2:参数未充分实例化17?-查找([2,3,4],4,Pos)。错误:is/2:参数没有充分实例化18?-find([2,3,4],5,Pos)。
Pos=notfound。

@hardmath,@Aleg,Bah,这就是我在没有测试的情况下编写它的原因。我现在已经修复了它,虽然我现在看到它不是最好的解决方案。find/3的第二条规则/子句中似乎有一个拼写错误。当您计算Pos时,变量Len没有被绑定,所以这里的意思可能是Len0(在前面的子目标中绑定)。
find([], _, notfound).
find([H|_], H, 1) :- !
find([_|T], H, Pos) :-
        find(T, H, Found),
        (
            integer(Found), !,
            Pos is Found+1
        );(
            Pos = notfound
        ).