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).
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
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am