# Labeling outputs of a function inductively

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.

• Shouldn't $f(1)$ be $10$ instead? Anyway, look up StringJoin[]. Commented Jun 2, 2020 at 14:40
• 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
– math
Commented Jun 2, 2020 at 14:50
• In words or formula, can you explicitly state the rules for this labeling? I cannot understand how you generate the labels. Commented Jun 2, 2020 at 15:27
• @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.
– math
Commented Jun 2, 2020 at 15:42

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:

SeedRandom[12345]
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}]


• 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.
– math
Commented Jun 2, 2020 at 15:10
• @math, does the new version give what you need?
– kglr
Commented Jun 2, 2020 at 15:43
• This seems to work, thanks!
– math
Commented Jun 3, 2020 at 8:17