PROLOG LANGUAGE: We squash a list L by first removing all lists within L and replacing them with their members then we r

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

PROLOG LANGUAGE: We squash a list L by first removing all lists within L and replacing them with their members then we r

Post by answerhappygod »

PROLOG LANGUAGE:
We squash a list L by first removing all lists within L andreplacing them with their members then weremove all duplicate values in the list. Write a Prolog rule squash/2 which squashesa list. The items in the output list do not have to be in anyspecific order.
Test file:
:- nl, write('squash/2:'), nl.:- write(' 1: '), squash([a,b,c,d,e,f],X), write(X),nl.:- write(' 2: '), squash([1,1,2,2,3,4,[5,6,[7,8]]],X),write(X), nl.:- write(' 3: '), squash([[2,4,6,8],1,2,[3,4,[5,6],7],8],X),write(X), nl.:- write(' 4: '), squash([[c],[s,c,h,e,m,e],[p,r,o,l,o,g]],X), write(X), nl.
My code doesn't pass all tests, I can't removeduplicates from the list. Can you tell me what needs to bechanged in the code so that it works correctly? Thank you.My code:squash([], []).squash([H|T],Result) :- squash(H,NewH), squash(T,NewT), append(NewH,NewT,Result).squash([H|T], [H|NewT]) :- H \= [], H \= [_|_], squash(T,NewT).
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply