Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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
在Prolog中从列表的每个子列表中删除最大元素_Prolog - Fatal编程技术网

在Prolog中从列表的每个子列表中删除最大元素

在Prolog中从列表的每个子列表中删除最大元素,prolog,Prolog,有人能帮我写一个函数,从列表的每个子列表中删除最大元素吗?我不知道。。。 到目前为止,我只知道如何编写delete子句: delete(_,[],[]). delete(E,[H|T],L):- delete(E,T,L). delete(E,[H|T],[H|T1):- delete(E,T,T1). 对于列表[1,2,4,3,4,5,6,7]==>[1,2,3,5,6]]这应该适用于任意嵌套的子列表。在这方面,这可能有些过分,但要求并不明确 % second list is

有人能帮我写一个函数,从列表的每个子列表中删除最大元素吗?我不知道。。。 到目前为止,我只知道如何编写delete子句:

delete(_,[],[]).
delete(E,[H|T],L):-
    delete(E,T,L).
delete(E,[H|T],[H|T1):-
    delete(E,T,T1).

对于列表[1,2,4,3,4,5,6,7]==>[1,2,3,5,6]]

这应该适用于任意嵌套的子列表。在这方面,这可能有些过分,但要求并不明确

% second list is the first list with sublists removed
remove_sublists([H|T], L) :-
    (   is_list(H)
    ->  L = T1
    ;   L = [H|T1]
    ),
    remove_sublists(T, T1).
remove_sublists([], []).

% max value in list ignoring sublists
% if L members are all lists, then this predicate fails
mymax(L, Max) :-
    remove_sublists(L, F),
    F \= [],
    max_list(F, Max).

% delete maximum value from every sublist,
% and sublists of sublists
delete_max_subs([H|T], [H1|T1]) :-
    (   is_list(H)
    ->  (   mymax(H, M)
        ->  delete(H, M, HD)
        ;   HD = H
        ),
        delete_max_subs(HD, H1)
    ;   H1 = H
    ),
    delete_max_subs(T, T1).
delete_max_subs([], []).
一些结果:

| ?- delete_max_subs([1,[2,4,3,4],5,[6,7]], L).

L = [1,[2,3],5,[6]]

yes
| ?- delete_max_subs([[[1,2],2],[2,4,3,4],5,[6,7]], L).

L = [[[1]],[2,3],5,[6]]

yes
| ?- delete_max_subs([[[1,2,3],2,5],[2,4,3,4],5,[6,8,7]], L).

L = [[[1,2],2],[2,3],5,[6,7]]

yes
附录

一个更加直观/Turbo Prolog友好的版本:

% second list is the first list with sublists removed
remove_sublists([H|T], T1) :-
    list(H),
    remove_sublists(T, T1).
remove_sublists([H|T], [H|T1]) :-
    not(list(H)),
    remove_sublists(T, T1).
remove_sublists([], []).

% max value in list ignoring sublists
mymax(L, Max) :-
    remove_sublists(L, F),
    F \= [],
    max_list(F, Max).    % I don't know if something like this exists in TP

% delete max recursively from L and all sublists
delete_max(L, R) :-
    mymax(L, M),
    delete(L, M, L1),
    delete_max_subs(L1, R).
delete_max(L, R) :-
    delete_max_subs(L, R).
delete_max([], []).

% delete maximum only from sublists
delete_max_subs([H|T], [H1|T1]) :-
    list(H),
    delete_max(H, H1),
    delete_max_subs(T, T1).
delete_max_subs([H|T], [H|T1]) :-
    not(list(H)),
    delete_max_subs(T, T1).
delete_max_subs([], []).

第三个filter/3谓词减少为:filterX、[H|T]、[H|C]:-X\=H、filterX、T、C。第二个和第三个deletemax/2子句也是如此。当第一个参数是单个元素列表时,不需要使用append/3。这是一个不错的解决方案+1,但它不能处理嵌套子列表的所有情况,例如,[[1,2],2],[2,4,3,4],5,[6,7]]。但是OP是否需要任意的子列表嵌套尚不清楚。是否需要处理多个级别的子列表,如[1、[2,4,3、[7,1]]、5、[[6]、[7,8]]?您好。你知道turbo prolog是否有->/2吗?@capelical我找不到任何证据表明turbo prolog支持->/2,所以我添加了一个不使用它的版本。真不错!我们可以理解->/2是多么有用@我同意/在我看来,2在很多情况下都能澄清逻辑,当然,如果使用正确的话:。
% second list is the first list with sublists removed
remove_sublists([H|T], T1) :-
    list(H),
    remove_sublists(T, T1).
remove_sublists([H|T], [H|T1]) :-
    not(list(H)),
    remove_sublists(T, T1).
remove_sublists([], []).

% max value in list ignoring sublists
mymax(L, Max) :-
    remove_sublists(L, F),
    F \= [],
    max_list(F, Max).    % I don't know if something like this exists in TP

% delete max recursively from L and all sublists
delete_max(L, R) :-
    mymax(L, M),
    delete(L, M, L1),
    delete_max_subs(L1, R).
delete_max(L, R) :-
    delete_max_subs(L, R).
delete_max([], []).

% delete maximum only from sublists
delete_max_subs([H|T], [H1|T1]) :-
    list(H),
    delete_max(H, H1),
    delete_max_subs(T, T1).
delete_max_subs([H|T], [H|T1]) :-
    not(list(H)),
    delete_max_subs(T, T1).
delete_max_subs([], []).