Answered step by step
Verified Expert Solution
Question
1 Approved Answer
DEAR CHEGG EXPERT, TOP BE DONE IN PROLOG ONLY PLEASE DO AS SOON AS POSSIBE. REALLY URGENT!!! QUESTION (SEE BELOW): WRONG ANSWER (GIVEN BY CHEGG
DEAR CHEGG EXPERT, TOP BE DONE IN PROLOG ONLY PLEASE DO AS SOON AS POSSIBE. REALLY URGENT!!!
QUESTION (SEE BELOW):
WRONG ANSWER (GIVEN BY CHEGG EXPERT - OUTPUT COMING WRONG):
2. In this assignment you can use all Prolog functions. (a) L1 and L2 are arbitrary numeric lists/trees. Make Prolog program addlst(L1, L2, Sum) where Sum is the sum of all components in L1 and L2. For example, the statement addlst ([[1], 2], [3/4]], [[1],S] should return s = 11. (b) List X contains n numbers x1, x2,...,xn. List Y contains k numbers yl, y2,...,yk. Write a Prolog program that that in the case n=k returns the value xl*yl+x2*y2+...xn*yn; in all other cases the program should return the error message Length error, as in the following examples: ?- sp ([1,2,3] , [3,2,2], S]. S = 10. ? - sp ([1,2,3,4], [3,2,1], S). S = 'Length error'. Expert Answer Was this answer helpful? 0 Anonymous answered this 971 answers Part (a) addist([H|T], Sum):- addist(H,Sum1), addlst(T,Sum2), Sum is Sumi+Sum2. addist([],0). addist(N,N):- number(N). addist(L1,L2, Sum):- addist(L1, Sum1), addist(L2, Sum2), Sum is Suml+Sum2. Part (b) sp(0.0],0). sp(L_I_1.0.'Length Error'). sp(0.L_I_],'Length Error'). sp([H1|T1],[H2/T2),Result):- P1 is H1*H2, sp(T1,72,Result2). number(Result2), Result is P1+Result2. sp([_|T1),L_|T2)'Length Error'):- sp(T1,72, Length Error"). Comment >Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started