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

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!

1 Like

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.

1 Like

OK, did it! Nice one, Jan!

Perhaps you should add a topic to Nice to know with example code and explaining how it works.

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

``````?- 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, _]].
``````
1 Like