Write two clauses in Prolog that determines if there are exactly two values in a
ID: 3720151 • Letter: W
Question
Write two clauses in Prolog that determines if there are exactly two values in a list such that they sum a value N. The output should be a single true if there exists two values whose sum is N or a single false if there are not. If there are multiple values whose sum is N then your program should only print true once.
Sample
List [1, 3, 5, 7] with N equal to 10 should print true. 7 + 3 = 10.
List [1, 3, 5, 7] with N equal to 20 should print false. No two values sum is 20.
List [7, 1, 10, 3, 8, 2] with N equal to 9 should print true. 2 + 7 = 9 and 8 + 1 = 9 and 1 + 8 = 9 and 7 + 2 = 9.
Restrictions
You are restricted to the following.
· Use of at most 2 clauses.
· +
· ,
· ;
· .
· !
· :-
· is
· Lists
· Head and tail syntax for list types
· Variables
Explanation / Answer
Answer::
list=integer*
findsum(list)
sum(list,integer)
clauses
findsum(L):-
sum(L,Sum),
write(" Sum Of Given List : ",Sum).
sum([],0).
sum([X|Tail],Sum):-
sum(Tail,Temp),
Sum=Temp+X.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.