# How to turn a list of tokens into a list of unbound variables?

**URL:** <https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776>\
**Category:** Help!\
**Created:** [December 15, 2021, 8:12pm UTC](https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776 "2021-12-15T20:12:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ataraxia](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ataraxia/32/3664_2.png) [@Ataraxia](https://swi-prolog.discourse.group/u/Ataraxia)\
**Post date:** [December 15, 2021, 8:12pm UTC](https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776/1 "2021-12-15T20:12:29Z")

</div>

Just thought I’d post a very quick question here, as I don’t use any online chat services. I’ve been catching up with some of the Advent of Code challenges and my few remaining brain cells are starting to struggle with some very simple stuff.

✅ I’m using: SWI-Prolog version 8.5.3 for Linux.

In this case, for example, I have a list like this:  
`[[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]]`

And I want it to turn into something like this:  
`[[A], [A, B], [A, B, C], [A, B, C, D]]`

For the challenge, I need to try to instantiate the variables depending on the sets that they come in. And I need to generate one unique variable per unique token.

I’m sure there’s some really easy, fundamental, Prological technique for doing this, but my head is fried at the minute and I can’t think of it.

Thanks!

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [December 15, 2021, 8:28pm UTC](https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776/2 "2021-12-15T20:28:33Z")

</div>

The rough idea is to create a mapping, for example using library(assoc). Now each time you find a token that must be a variable, you look it up in the assoc and replace it by the (variable) value. If it isn’t there you add a new binding from the value to a new variable.

---

<div class="post-metadata">

**Author:** ![Ataraxia](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/ataraxia/32/3664_2.png) [@Ataraxia](https://swi-prolog.discourse.group/u/Ataraxia)\
**Post date:** [December 16, 2021, 10:21am UTC](https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776/3 "2021-12-16T10:21:33Z")

</div>

OK, did it! Nice one, Jan!

---

<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:** [December 16, 2021, 10:30am UTC](https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776/4 "2021-12-16T10:30:07Z")

</div>

Perhaps you should add a topic to [Nice to know](https://swi-prolog.discourse.group/c/nice-to-know/17) with example code and explaining how it works. 😉

---

<div class="post-metadata">

**Author:** ![oskardrums](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/oskardrums/32/3594_2.png) [@oskardrums](https://swi-prolog.discourse.group/u/oskardrums)\
**Post date:** [December 16, 2021, 10:44am UTC](https://swi-prolog.discourse.group/t/how-to-turn-a-list-of-tokens-into-a-list-of-unbound-variables/4776/5 "2021-12-16T10:44:41Z")

</div>

Note that in the special case of mapping natural numbers to variables, you could also use varnumbers/2 like so:

```prolog
?- maplist(
| [In, Out]>>maplist([N, '$VAR'(N)]>>true, In, Out), % N --> '$VAR'(N)
| [[1], [1, 2], [1, 2, 3], [1, 2, 3, 4]], _Vs), varnumbers(_Vs, V).
V = [[_A], [_A, _B], [_A, _B, _C], [_A, _B, _C, _]].

```
