Sum of even, product of odd numbers in Prolog

后端 未结 4 2064
悲&欢浪女
悲&欢浪女 2020-12-21 09:33

I have a list of numbers, I need to calculate the sum of the even numbers of the list and the product of the odd numbers of the same list. I\'m new in Prolog, and my searche

4条回答
  •  不知归路
    2020-12-21 09:57

    untested!

    sum_odd_product_even([], S, P, S, P).
    sum_odd_product_even([H|T], S0, P0, S, P) :-
        S1 is S0 + H,
        sum_even_product_odd(T, S1, P0, S, P).
    
    sum_even_product_odd([], S, P, S, P).
    sum_even_product_odd([H|T], S0, P0, S, P) :-
        P1 is P0 * H,
        sum_odd_product_even(T, S0, P1, S, P).
    
    sum_odd_product_even(L, S, P) :-
        sum_odd_product_even(L, 0, 1, S, P).
    
    sum_even_product_odd(L, S, P) :-
        sum_even_product_odd(L, 0, 1, S, P).
    

提交回复
热议问题