PROLOG LANGUAGE: For example output: squash([1,2,3,4,[5,6,[7,8]]], [1,2,3,4,5,6,7,8]). We squash a list L by first remov

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: For example output: squash([1,2,3,4,[5,6,[7,8]]], [1,2,3,4,5,6,7,8]). We squash a list L by first remov

Post by answerhappygod »

PROLOG LANGUAGE:
For example output: squash([1,2,3,4,[5,6,[7,8]]], [1,2,3,4,5,6,7,8]).
We squash a list L by first removing all lists within L and replacing them with their members then we remove all duplicate values in the list. Write a Prolog rule squash/2 which squashes a list. The items in the output list do not have to be in any specific order.
Helper:
You can 'flatten' a list by removing all the square brackets around any lists it contains as elements, and around any lists that its elements contain as elements, and so on for all nested lists.
1. Do a recuvise call to find the uniqueness in the list using member
uniques([H | T], T1) :- use member to check unique number
uniques([H | T], [H | T1]) :-
2. squash(X, Y) :- flatten(X, X1), uniques(X1, Y).
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply