Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[1 mark] Write a predicate myAppend(L1,L2,L3) to append 2 lists (order is import

ID: 3812550 • Letter: #

Question

[1 mark] Write a predicate myAppend(L1,L2,L3) to append 2 lists (order is important, you may not use the built in append). E.g.,

[1 mark] Using your definition of myAppend, write a predicate myFirst(X,L) that is true if X is the first item in L.

[2 mark] Rewrite the predicate myLast(X,L) from the previous question using append.

[3 marks] Rewrite the predicate nextto(X,Y,L) from the previous question using append.

[3 marks] Write a recursive predicate myReverse(L1,L2) that succeeds when the result of reversing the elements of list L1 is the list L2. Your answer should use append, and may not make use of the built in reverse predicate.

Explanation / Answer

1)
'appending two lists to L3
myAppend(L1,L2,L3) :-
   append(L1,L2,L3)
      
----------------------------------------------------------------------------------------------------------------------------------------------------------
2)
' it will checks whether X is in list and it is first element
myFirst(X,L) :-
   member(X,[X|L])

--------------------------------------------------------------------------------------------
3)
' it will checks whether X is in list and it is last element
myLast(X,L) :-
   member(X,L)
  
--------------------------------------------------------------------------------------------------------------------------------------------------
4)  
' check x and y in list
nextto(X,Y,L) :-
   member(X,[Y|L])
-------------------------------------------------------------------------------------------------------------------------------------------------
5)
'check reverse of list is equal to second list
myReverse(L1,L2) :-
   myrev([], []).
   myrev([Head|[]], [Head]).
   myrev([Head|Tail], finalList) :-
   myrev(Tail, L1),
   append(L1, [Head], finalList)
  
   'check reversed list is equal to L2 or not
   finalList = L2

------------------------------------------------------------------------------------------------------------------------------------------------------------------
*** directly reverse function available in prolog which can do this problem in single statement..
myReverse(L1,L2) :-
   reverse(L1,L2)  
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote