Suppose I have two lists labeled by the string "0" and "1" respectively, and two functions $f$ and $g$ which takes as input these lists. Is there a way that I can inductively label the outputs of these with binary strings?

For example, suppose I apply $f$ to a list labelled with a string $``w"$. I would like $f(``w")$ to be labelled with the string $``w0"$, and similarly $g(w)$ labelled with $``w1"$.

More explicitly,

Start with two lists list0 and list1. I want these to be labelled by the string "0" and "1" respectively. Then if I apply $f$ to list0, I want Mathematica to label this output with the string "00". If I apply $f$ to list1, I want Mathematica to label this output with the string "10"

Similarly if I apply $g$ to list0, I want it labelled "01" and g(list1) to be labelled "11".

This is really about labelling output lists in an iterative way.

  • $\begingroup$ Shouldn't $f(1)$ be $10$ instead? Anyway, look up StringJoin[]. $\endgroup$ Commented Jun 2, 2020 at 14:40
  • $\begingroup$ Thanks @J.M., fixed it now. I have used StringJoin to create all possible lists on length n but I'm more concerned with how we can inductively label lists using these outputs $\endgroup$
    – math
    Commented Jun 2, 2020 at 14:50
  • $\begingroup$ In words or formula, can you explicitly state the rules for this labeling? I cannot understand how you generate the labels. $\endgroup$
    – MarcoB
    Commented Jun 2, 2020 at 15:27
  • $\begingroup$ @MarcoB I have tried to clarify what I mean. I have functions that give lists as an output, but I somehow want Mathematica to label these outputs in an inductive way. $\endgroup$
    – math
    Commented Jun 2, 2020 at 15:42

1 Answer 1


You can use StringPadRight as follows:

ClearAll[f, g, labeling]
SetAttributes[labeling, HoldAll]
labeling[h_[x_]] := StringPadRight[x, 1 + StringLength@x, h /. {f -> "0", g -> "1"}];


examples = StringJoin @@@ Table[RandomChoice[{"0", "1"}, RandomInteger[{1, 8}]], 10];

TableForm[{examples , labeling[f@#] & /@ examples, labeling[g@#] & /@ examples}, 
 TableHeadings -> { {"x", "labeling[f[x]]", "labeling[g[x]]"}, None}]

enter image description here

  • $\begingroup$ Thank you for the answer but I'm not sure my question was clear enough. I was using the labeling as an example. What I mean is let list1 = "0" and list2 = "1". Then I want f(list1) to be labelled 01 and g(list2) labelled 11 etc. I have generated words on length $n$ on binary symbols but what I want is to assign these to outputs. $\endgroup$
    – math
    Commented Jun 2, 2020 at 15:10
  • $\begingroup$ @math, does the new version give what you need? $\endgroup$
    – kglr
    Commented Jun 2, 2020 at 15:43
  • $\begingroup$ This seems to work, thanks! $\endgroup$
    – math
    Commented Jun 3, 2020 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.