# Value\_list/3 - Construct a list of same value

**URL:** <https://swi-prolog.discourse.group/t/value-list-3-construct-a-list-of-same-value/5303>\
**Category:** Nice to know\
**Created:** [April 19, 2022, 9:26am UTC](https://swi-prolog.discourse.group/t/value-list-3-construct-a-list-of-same-value/5303 "2022-04-19T09:26:18Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [April 19, 2022, 9:40am UTC](https://swi-prolog.discourse.group/t/value-list-3-construct-a-list-of-same-value/5303/3 "2022-04-19T09:40:39Z")

</div>

> [@jan](#):
>
> What about this?

Thanks. 🙂

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.

* * *

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

```

* * *

Example usage:

```prolog
?- 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.

```prolog
?- 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.

```prolog
?- 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)](https://www.swi-prolog.org/pldoc/man?section=yall) a Lambda can be used, e.g.

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

```

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

```prolog
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](https://github.com/SWI-Prolog/swipl-devel/blob/963067bc72b4af7caeaad5693e010cde2e325846/library/yall.pl)) 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.

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

```

* * *

---

_[View the full topic](https://swi-prolog.discourse.group/t/value-list-3-construct-a-list-of-same-value/5303)._
