Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi CHEGG EXPERT PLEASE CORRECT THE ERROR For the Question below for PART A and B ONLY : Question (See Below): 2. In this assignment

Hi CHEGG EXPERT PLEASE CORRECT THE ERROR For the Question below for PART A and B ONLY:

Question (See Below):

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 y1, y2,...,yk. Write a Prolog program that that in the case n=k returns the value x1*y1+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, 1], S]. S = 10. ?- sp([1,2,3,4], [3,2,1], S). S = Length error.

ERROR FOR Part B:

ERROR: Syntax error: Illegal start of term ERROR: sp([1,2,3],[3,2,2], ERROR: ** here ** ERROR: S] . PART B CODE (GIVEN BY CHEGG EXPERT): sp([],[],0). sp([_|_],[],'Length Error'). sp([],[_|_],'Length Error'). sp([H1|T1],[H2|T2],Result):- P1 is H1*H2, sp(T1,T2,Result2), number(Result2), Result is P1+Result2. sp([_|T1],[_|T2],'Length Error'):- sp(T1,T2,'Length Error'). ERROR FOR Part A (OUTPUT COMING WRONG, iT SHOULD COME OUT TO BE 11 when running the code : addlst([[1],2],[3|4]],[[1]],S] )

?- ['/Users/mac2020/Desktop/addlst.pl']. true. ?- addlst([[1], 2], [3|4]], [[1],S] | | | | | | | | | |

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions