Value_list/3 - Construct a list of same value

Thanks. :slightly_smiling_face:

I learn more from you by showing my wrong ways then you showing me the right way.



Here is the original post where I did it the harder way before Jan W. showed the better way.

I leave it here because it is one of the few simple working examples of libary(yall) using bracy list.


value_list(Length,Value,L) :-
    length(L0,Length),
    maplist({Value}/[_,Value]>>true,L0,L).

Example usage:

?- value_list(5,0,L).
L = [0, 0, 0, 0, 0].

How this works.

length/2 is often used to generate list of variables of a given size, e.g.

?- length(L,5).
L = [_, _, _, _, _].

Normally when a list of variables is displayed, if the variable is not bound to a value it is displayed as _ but the variables really are different variables, e.g.

?- length(L,5),numbervars(L).
L = [A, B, C, D, E].

Now to get the desired result (list of values) the value just needs to be bound to each variable. maplist/3 should come to mind.

maplist/3 is often written using a helper predicate for the goal but using library(yall) a Lambda can be used, e.g.

maplist([_,0]>>true,L0,L).

However if one tries to use a variable for the value, e.g.

maplist([_,Value]>>true,L0,L).

it will not work as expected.

With library(yall) one has to be careful with the variables used in the Lambda. Under the coves library(yall) (src) makes use of copy_term_nat/2 but library(yall) also provides a means to resolve the problem.

A bracy list {...} specifies which variables are shared between the wrapped goal and the surrounding context.

Thus Value can be shared outside of the Lambda and inside the Lambda when identified in the bracy list, i.e. {Value}, e.g.

maplist({Value}/[_,Value]>>true,L0,L)