Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List processing

Status
Not open for further replies.

Samantha2323

Programmer
Apr 17, 2010
6
US
Hello I need help on doing this .... I have no idea how to start up.... can you please guide me a little bit

The map_pairs_to_kval_list predicate takes a list of <key,value> pairs and generates a list of values for each key, <key, val1, val2,...>, preserving the order of keys and values. For example:

?- map_pairs_to_kval_list([[jack,a], [jack,b], [jack,f], [jill,cplus],[jill,aminus], [jack,a],[jack,w],[jill,aminus]], X).
X = [[jack,a,b,f,a,w],[jill,cplus,aminus,aminus]]
 
% map_pairs_to_kval_list - the required predicate
% map_pairs_same - a helping predicate that works like the
% previous one, except it can only deal with pairs where
% the first member is the same

% map_pairs_same([[a, b], [a, c], [a, d], [a, e]], R).
% R = [b, c, d, e]

% remove - a helping predicate that removes all occurences
% of elements of a list in another list

% remove([1, 2, 3, 4, 5, 6, 2, 3], [2, 3], R).
% R = [1, 4, 5, 6]

map_pairs_to_kval_list([], []).
map_pairs_to_kval_list(List, [ResultHead | ResultTail]) :-
List = [Head | _],
Head = [A, _],
findall([A, B], member([A, B], List), Candidates),
map_pairs_same(Candidates, ResultHeadPartial),
remove(List, Candidates, NewList),
ResultHead = [A | ResultHeadPartial],
map_pairs_to_kval_list(NewList, ResultTail).

map_pairs_same([], []).
map_pairs_same(List, [B | ResultTail]) :-
List = [Head | Tail],
Head = [_, B],
map_pairs_same(Tail, ResultTail).


remove(List, [], List).
remove(List, [X | Tail], Result) :-
delete(List, X, List1),
remove(List1, Tail, Result).
 
Another way to do that is to walk through the list and process each element. We walk from the end to the beginning of the list, return from recursion :
Code:
% if the list is empty, nothing to do
map_pairs_to_kval_list([], []).

% else we process the rest of the list
% and we insert the first element in the result list
map_pairs_to_kval_list([H | T], L) :-
	map_pairs_to_kval_list(T, L1),
	insert(H, L1, L).
Now, we must insert H in the key-value list.
We have 3 args : the element, the current key-value list, and the result key-value list.

3 cases may appear

1- the result list is empty, we create it.
Code:
insert([Key, Val], [], [[Key, Val]]).
2 - the element is the first of the result list, we add its value as the first element of the values
Code:
insert([Key, Val], [[Key | LstVal] | T], [[Key, Val | LstVal] | T]) :- !.

3- if the key is not at the begining of the list, we insert it in the rest :
Code:
insert([Key, Val], [H | T], [H | T1]) :-
	insert([Key, Val], T, T1).


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top