combinations - generate a big list from a given list by enumeration in prolog -


i trying generate big list (2^n elements) small given list (n elements). example, there list [x,y,z], in x, y or z 0.1 or 0.9. pick 0.1 or 0.9 x, pick 0.1 or 0.9 y , pick 0.1 or 0.9 z sequentially, see xyz element of new list. there should 2^n elements in new list [0.001,0.009,0.009,...,0.729] ([0.1*0.1*0.1, 0.1*0.1*0.9, 0.1*0.9*0.1, ..., 0.9*0.9*0.9]). how can new list given list? n parameter acquired given list,

trn(list_given,outlist):-     length(list_given,n),     ... 

i want implement this,

?- trn([x,y],out). out=[0.01,0.09,0.09,0.81]. 

thanks in advance!

to generate possible combinations of values:

mem(l, e) :- member(e, l).  gen_values(n, choices, values) :-     length(values, n),     maplist(mem(choices), values). 

so:

| ?- gen_values(3, [0.1, 0.9], l).  l = [0.10000000000000001,0.10000000000000001,0.10000000000000001] ?  l = [0.10000000000000001,0.10000000000000001,0.90000000000000002]  l = [0.10000000000000001,0.90000000000000002,0.10000000000000001]  l = [0.10000000000000001,0.90000000000000002,0.90000000000000002]  l = [0.90000000000000002,0.10000000000000001,0.10000000000000001]  l = [0.90000000000000002,0.10000000000000001,0.90000000000000002]  l = [0.90000000000000002,0.90000000000000002,0.10000000000000001]  l = [0.90000000000000002,0.90000000000000002,0.90000000000000002]  (1 ms) yes 

then need list multiplier:

prodlist(l, p) :-     prodlist(l, 1, p).  prodlist([x], a, p) :- p x * a. prodlist([x,y|t], a, p) :-     a1 x * a,     prodlist([y|t], a1, p). 

then can of results findall/3:

trn(list_given, outlist):-     length(list_given, n),     findall(x, (gen_values(n, [0.1, 0.9], values), prodlist(values, x)), outlist). 

resulting in:

| ?- trn([_,_,_], out).  out = [0.0010000000000000002,0.0090000000000000028,0.0090000000000000011,0.081000000000000016,0.0090000000000000011,0.081000000000000016,0.081000000000000016,0.72900000000000009]  yes | ?- 

you can use trn([x,y,z], out) here, there's no point. define predicate length: trn(3, out).

note, there's slight floating point precision inaccuracy happening, yields long decimals. also, method doesn't take advantage of redundancy in calculations, it's not efficient approach. , finally, if n (the length of input list) large, explode.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -