I have a list of strings consisting of sequences of "+" or "-"'s. For example, let's take the list

lst={"-+", "+-+", "--+", "++-+", "+--+", "---+"}

I also have four functions $f_- (a),f_+ (a),s_- (a),s_+ (a)$ that take as arguments lists of four elements $a=\{x,y,z,w\}$. The functions $s_\pm$ return a number, while the functions $f_\pm$ return another list $\{x',y',z',w'\}$.

I want to write a function that reads each item from lst and creates iterated expressions of the form $$s_-(a) \, s_+ (f_- (a)),\\ s_+(a)\,s_- (f_+ (a))\,s_+(f_-(f_+(a))),\\ s_-(a)\,s_- (f_- (a))\,s_+(f_-(f_-(a))),\ldots$$ for the first three terms for example.

So it works like this: (i) for the first symbol it returns the $s$ function with the same subscript, (ii) for the second symbol it returns again the $s$ function of the same symbol but with argument the $f$ function with subscript of the previous symbol, (iii) for the third symbol it returns the $s$ function with the same subscript but with argument nested $f$ functions with subscripts matching the previous two symbol, (iv) .... The rule continues until we read all the symbols of the string.

Is there a relatively simple way to do this? Thanks!

  • $\begingroup$ What have you tried so far? Why do you need this? What are you going to do with the results? $\endgroup$
    – MarcoB
    Nov 16, 2023 at 15:03
  • $\begingroup$ I don't know how to approach this so I haven't tried much. I did try to make a modification of @kglr 's answer but failed so far. The rest is not that relevant, I would think. $\endgroup$
    – AG1123
    Nov 17, 2023 at 3:24
  • $\begingroup$ It is often important to establish context to a question. This attempts to avoid falling into the XY Problem. When the task looks odd, there is always a chance that a completely different (and easier) solution to the problem may exist, but it has just not occurred to you. That's why providing context to the question, both looking back (where you come from) and looking forward (how you want to use the solution) may lead to better answers in the long run. $\endgroup$
    – MarcoB
    Nov 17, 2023 at 15:47

1 Answer 1


Not sure if this is what you need... If it is not, I will delete.

Format[s[arg : ("+" | "-")], StandardForm] := Subscript[s, arg]
Format[f[arg : ("+" | "-")], StandardForm] := Subscript[f, arg]
Format[s[arg : ("+" | "-")], TraditionalForm] := Subscript[s, arg]
Format[f[arg : ("+" | "-")], TraditionalForm] := Subscript[f, arg]


fpl = Inactive[Times] @@ 
  FoldPairList[{s[#2] @ #1, f[#2] @ #} &, a, Characters @ #] &;


lst = {"-+", "+-+", "--+", "++-+", "+--+", "---+"}

fpl /@ lst // Column // TraditionalForm // TeXForm

$$\begin{array}{l} s_-(a)\times s_+\left(f_-(a)\right) \\ s_+(a)\times s_-\left(f_+(a)\right)\times s_+\left(f_-\left(f_+(a)\right)\right) \\ s_-(a)\times s_-\left(f_-(a)\right)\times s_+\left(f_-\left(f_-(a)\right)\right) \\ s_+(a)\times s_+\left(f_+(a)\right)\times s_-\left(f_+\left(f_+(a)\right)\right)\times s_+\left(f_-\left(f_+\left(f_+(a)\right)\right)\right) \\ s_+(a)\times s_-\left(f_+(a)\right)\times s_-\left(f_-\left(f_+(a)\right)\right)\times s_+\left(f_-\left(f_-\left(f_+(a)\right)\right)\right) \\ s_-(a)\times s_-\left(f_-(a)\right)\times s_-\left(f_-\left(f_-(a)\right)\right)\times s_+\left(f_-\left(f_-\left(f_-(a)\right)\right)\right) \\ \end{array}$$


ClearAll[s0, s1, f0, f1]

fpl2 = Times @@ 
    FoldPairList[{Symbol["s" <> #2]@#1, Symbol["f" <> #2]@#} &, a, 
     Characters@#] &;

lst2 = lst // StringReplace[{"-" -> "0", "+" -> "1"}];

functionlist = fpl2 /@ lst2;

functionlist // Column

enter image description here

Column @ 
  {f0 = (2 # &), 
   f1 = (Sin[#] &),
   s0 = Plus, 
   s1 = Times, 
   a = {x, y, z, w}},
  Evaluate @ functionlist]

enter image description here

  • $\begingroup$ Thank you for you answer. It's quite nice but this is not what I meant. Perhaps I didn't explain it very well. I will edit my question. It's not about generating the latex text but actually generating the functions in Mathematica. In other words, given some definition of the $f$ and $s$ functions in Mathematica and the lst list of strings, I want to generate a list where all these nested functions are evaluated. Is it possible to modify your code to do this? I couldn't do it immediately. Thanks $\endgroup$
    – AG1123
    Nov 17, 2023 at 3:22
  • 1
    $\begingroup$ @AG1123, simply replace Inactive[Times] with Times. $\endgroup$
    – kglr
    Nov 17, 2023 at 3:37
  • $\begingroup$ Thanks. Yes I noticed that but I a still have some issues with the subscripts and the definition of the functions. Could you tell me how I could get rid of the "+" and "-" as subscripts? Perhaps map "-" to "0" and "+" to "1" and take this as part of the name of the functions. Then, instead of having to deal with $f_-, f_+$ etc I would have the function defined as $f0,f1$ etc. What should I modify to get this behavior? Thanks $\endgroup$
    – AG1123
    Nov 17, 2023 at 3:53
  • 1
    $\begingroup$ @AG1123, please see the update. $\endgroup$
    – kglr
    Nov 17, 2023 at 4:23
  • 1
    $\begingroup$ Thanks a lot! That is what I was looking for. I will accept your answer $\endgroup$
    – AG1123
    Nov 17, 2023 at 4:49

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .