Prolog:试图使奇数甚至列表的特定要素

发布于 2025-01-23 19:55:21 字数 954 浏览 0 评论 0原文

我想知道列表中存在多少个偶数和奇数,直到用户输入的特定索引。

以下代码正常工作,但仅给出偶数或奇数元素。有人可以帮助我获得所需的输出吗? 提前致谢。

:- use_module(library(clpfd)).
oddlength(L):-

    L9 is L+2,
    L6 is L9/2,
    write('List has '),
    write(L6),
    write(' Odd Element').
evenlength(L):-
    L6 is L/2,
    write('List has '),
    write(L6),
    write(' Even Element').
split(Index,List,Left,Right) :-
   length(Left,Index),       % Actually CREATES a list of fresh variables if "Left" is unbound
   append(Left,Right,List).  % Demand that Left + Right = List.
   write(List).
create(L1):-read(Elem),create(Elem,L1).
create(-1,[]):-!. create(Elem,[Elem|T]):-read(Nextel),create(Nextel,T).
chklst([H|T]):-
length(T,L),
 L>=0 ->
 (
  L1 is L+1,
  L2 is mod(L1,2),
  L2=:=0 ->
   evenlength(L1)
  ;
   oddlength(L)
 ). 
go:- write('Creating a list'),nl, write('Enter -1 to stop'),nl, create(L), nl, write('Enter index'),read(ID),split(ID,L,X1,X2),nl, chklst(X1).

I want to know how many even and odd numbers are present in the list up to a specific index entered by the user.

The following code is working but only gives either even or odd elements. Can someone help me getting the desired output?
Thanks in advance.

:- use_module(library(clpfd)).
oddlength(L):-

    L9 is L+2,
    L6 is L9/2,
    write('List has '),
    write(L6),
    write(' Odd Element').
evenlength(L):-
    L6 is L/2,
    write('List has '),
    write(L6),
    write(' Even Element').
split(Index,List,Left,Right) :-
   length(Left,Index),       % Actually CREATES a list of fresh variables if "Left" is unbound
   append(Left,Right,List).  % Demand that Left + Right = List.
   write(List).
create(L1):-read(Elem),create(Elem,L1).
create(-1,[]):-!. create(Elem,[Elem|T]):-read(Nextel),create(Nextel,T).
chklst([H|T]):-
length(T,L),
 L>=0 ->
 (
  L1 is L+1,
  L2 is mod(L1,2),
  L2=:=0 ->
   evenlength(L1)
  ;
   oddlength(L)
 ). 
go:- write('Creating a list'),nl, write('Enter -1 to stop'),nl, create(L), nl, write('Enter index'),read(ID),split(ID,L,X1,X2),nl, chklst(X1).

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

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

发布评论

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

评论(1

桃酥萝莉 2025-01-30 19:55:21

似乎。 。 。复杂的。

首先,您需要的是测试整数是否奇怪甚至是:

even(N) :- 0 =:= N rem 2 .

一旦有了即可穿越列表的简单问题。

如果您想要的只是计数:

evens_and_odds( Ns , I, E, O ) :- evens_and_odds( Ns, 0, I, 0 , 0 , E, O ) .

evens_and_odds( []     , _ , _ , E , O , E , O ) .  % if we've exhausted the source list, we're done.
evens_and_adds( [N|Ns] , P , P , E , O , E , O ) .  % ditto, if we've hit the desired index.
evens_and_odds( [N|Ns] , P , Q , X , Y , E , O ) :- % otherwise....
  P < Q ,                                           % - we haven't yet hit the target index
  P1 is P+1,                                        % - tick the counter
  tally(N,X,Y,X1,Y1),                               % - do the tally
  evens_and_odds(Ns,P1,Q,X1,Y1,E,O)                 % - and recurse down
  .                                                 % Easy!

tally( N , E , O , E1 , O  ) :- even(N), !, E1 is E+1 .
tally( N , E , O , E  , O1 ) :-             O1 is O+1 . 

That seems . . . complicated.

First thing you need is a predicate to test whether an integer is odd or even:

even(N) :- 0 =:= N rem 2 .

Once you have that it's a simple matter of traversing the list.

If all you want are counts:

evens_and_odds( Ns , I, E, O ) :- evens_and_odds( Ns, 0, I, 0 , 0 , E, O ) .

evens_and_odds( []     , _ , _ , E , O , E , O ) .  % if we've exhausted the source list, we're done.
evens_and_adds( [N|Ns] , P , P , E , O , E , O ) .  % ditto, if we've hit the desired index.
evens_and_odds( [N|Ns] , P , Q , X , Y , E , O ) :- % otherwise....
  P < Q ,                                           % - we haven't yet hit the target index
  P1 is P+1,                                        % - tick the counter
  tally(N,X,Y,X1,Y1),                               % - do the tally
  evens_and_odds(Ns,P1,Q,X1,Y1,E,O)                 % - and recurse down
  .                                                 % Easy!

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