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 squashes alist. The items in the output list do not have to be in anyspecific order.
***** Helper: *****1. Do a recuvise call to find the uniqueness inthe list using memberuniques([H | T], T1) :- use member to check unique numberuniques([H | T], [H | T1]) :-2. squash(X, Y) :- flatten(X, X1), uniques(X1,Y).FYI…. You can 'flatten' a list by removing all the square bracketsaround any lists it contains as elements, and around any lists that its elementscontain as elements, and so on for all nested lists.
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.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply