Page 1 of 1

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

Posted: Fri Jul 08, 2022 6:35 am
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).