带序言的列表中最长的偶数序列

发布于 2025-01-16 01:01:53 字数 702 浏览 0 评论 0原文

我想实现一个谓词 Even_sequence(X,Y,Z),其中 X 是给定列表,Y 是偶数序列最大长度的计数器,Z 是存储当前偶数子序列长度的计数器。例如,对于给定列表 2,4,6,3,5,2,2 我想返回 3 因为 2,4,6 > 是最长的序列。

这是我尝试过的代码,但我不知道如何使其工作(我是 Prolog 的初学者)。如果我运行 even_sequence([2,2,2,3,3],C, R) 我收到错误:参数未充分实例化 In: [1] Even_sequence([2,2|...],_1730,_1732)

even_sequence([],_,_).
even_sequence([H|T],GlobalMax,LocalMax):-
    H mod 2 =:= 0,
    LocalMax1 is LocalMax+1,
    even_sequence(T,GlobalMax,LocalMax1).
even_sequence([H|T],GlobalMax,LocalMax):-
    H mod 2 =\= 0,
    GlobalMax1 is LocalMax,
    LocalMax1 is LocalMax-LocalMax,
    even_sequence(T,GlobalMax1,LocalMax1).
    
    

I want to implement a predicate even_sequence(X,Y,Z) where X is the given list, Y is a counter of the max length of even numbers sequence, and Z is the counter that stores the length of the current even number subsequence. For example for the given list 2,4,6,3,5,2,2 i want to return 3 because 2,4,6 is the longest sequence.

Here is the code that i tried and i don't know how to make it work(i am a total beginner in Prolog).If i run even_sequence([2,2,2,3,3],C,R) i get the error: Arguments are not sufficiently instantiated In: [1] even_sequence([2,2|...],_1730,_1732)

even_sequence([],_,_).
even_sequence([H|T],GlobalMax,LocalMax):-
    H mod 2 =:= 0,
    LocalMax1 is LocalMax+1,
    even_sequence(T,GlobalMax,LocalMax1).
even_sequence([H|T],GlobalMax,LocalMax):-
    H mod 2 =\= 0,
    GlobalMax1 is LocalMax,
    LocalMax1 is LocalMax-LocalMax,
    even_sequence(T,GlobalMax1,LocalMax1).
    
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩动你心 2025-01-23 01:01:53
longest_even_sequence(Lst, EvenSeqLen) :-
    % Keep running total of current sequence, and best-so-far
    longest_even_sequence_(Lst, 0, 0, EvenSeqLen).

longest_even_sequence_([], Cur, Best, EvenSeqLen) :-
    % Check final sequence, and assign EvenSeqLen
    num_num_max(Cur, Best, EvenSeqLen).

longest_even_sequence_([H|T], Cur, Best, EvenSeqLen) :-
    0 is H mod 2, !,
    % Is even, so sequence gets longer
    Cur1 is Cur + 1,
    longest_even_sequence_(T, Cur1, Best, EvenSeqLen).

longest_even_sequence_([_H|T], Cur, Best, EvenSeqLen) :-
    % Is odd (because not even), so check the finished sequence
    num_num_max(Cur, Best, Best1),
    % Starting new sequence
    longest_even_sequence_(T, 0, Best1, EvenSeqLen).


% Convenient wrapper
num_num_max(X, Y, Max) :-
    Max is max(X, Y).

swi-prolog 中的结果:

?- longest_even_sequence([2,4,6,3,5,2,2], N).
N = 3.

?- longest_even_sequence([1,2,3], N).
N = 1.
longest_even_sequence(Lst, EvenSeqLen) :-
    % Keep running total of current sequence, and best-so-far
    longest_even_sequence_(Lst, 0, 0, EvenSeqLen).

longest_even_sequence_([], Cur, Best, EvenSeqLen) :-
    % Check final sequence, and assign EvenSeqLen
    num_num_max(Cur, Best, EvenSeqLen).

longest_even_sequence_([H|T], Cur, Best, EvenSeqLen) :-
    0 is H mod 2, !,
    % Is even, so sequence gets longer
    Cur1 is Cur + 1,
    longest_even_sequence_(T, Cur1, Best, EvenSeqLen).

longest_even_sequence_([_H|T], Cur, Best, EvenSeqLen) :-
    % Is odd (because not even), so check the finished sequence
    num_num_max(Cur, Best, Best1),
    % Starting new sequence
    longest_even_sequence_(T, 0, Best1, EvenSeqLen).


% Convenient wrapper
num_num_max(X, Y, Max) :-
    Max is max(X, Y).

Result in swi-prolog:

?- longest_even_sequence([2,4,6,3,5,2,2], N).
N = 3.

?- longest_even_sequence([1,2,3], N).
N = 1.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文