Write a Prolog program that takes a list as its first argume

Write a Prolog program that takes a list as its first argument and returns the odd numbers and the sum of the odds as the second and third arguments respectively. For example sum_odd ([3, 4, 5, 7, 14], Odd_numbers, Sum). Odd_numbers = [3, 5, 7]. Sum = 15.

Solution

domains
sum=integer
   list=integer*
   Oddlist=integer*

predicates
   sumoddlist(list,oddlist,sum)
  
clauses

  
   sumoddlist(list,Oddlist,sum):-
       write(\"Odd Numbers=\",Oddlist),nl,
       write(\"Sum=\",sum),nl.
  
   sumoddlist([H|T],Oddlist,sum):-
       H mod 2 is 1,
       sum =sum+s,
       add_tail(T,H,oddlist),
       sumoddlist(T,Oddlist,sum).

 Write a Prolog program that takes a list as its first argument and returns the odd numbers and the sum of the odds as the second and third arguments respective

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site