I am searching some swi-prolog predicate:
list_to_dict(List, Dict) :- ??
Which is able to convert a list like
l = [a, b, c, d]
into a Prolog dict d
whose keys are equal to the list indexes starting at 1. In this example above d
is of form:
d = dict_label{1:a,2:b,3:c,4:d}
I would appreciate, if you can help me to find some general and hopefully short solution of that problem.
CodePudding user response:
You can use the following predicate:
% list_to_dict( Values, Tag, -Dict)
list_to_dict(Values, Tag, Dict) :-
findall(Key-Value, nth1(Key, Values, Value), Pairs),
dict_create(Dict, Tag, Pairs).
Example:
?- list_to_dict([a,b,c,d], dict_label, Dict).
Dict = dict_label{1:a, 2:b, 3:c, 4:d}.