Working within DynamicModule, how can a list of strings as be used to assign initial values to variables? For example, what nested functions can be used as f in the expression:

DynamicModule[f[{"a0=1.", "a1=1"}], {}]

One can imagine that some nested sequence of functions such as SetAttributes, Hold, ToExpression could be used....

Obviously, I can provide more background to this question if needed.

  • 2
    $\begingroup$ How (or why) are you planning to use this solution. Feels like an X Y problem. $\endgroup$ – Edmund Mar 6 '20 at 22:05
  • $\begingroup$ The syntax you propose is inappropriate for DynamicModule which requires a list as its first argument. $\endgroup$ – m_goldberg Mar 6 '20 at 22:41
  • $\begingroup$ Related: (19758) $\endgroup$ – Mr.Wizard Mar 8 '20 at 15:25

I think this does what you want:


DynamicModule[f[s : {__String}], body_] ^:=
  Join @@ MakeExpression@s /. _[set__] :> DynamicModule[{set}, body]

DynamicModule[f[{"a0=1.", "a1=1"}], {a0, a1}]
{1., 1}
  • $\begingroup$ Thank you very much Mr Wizard and kglr. It's an honor to have your thoughts on this. Very clean approach. $\endgroup$ – IntellectualDilettante Mar 9 '20 at 16:55
h /: DynamicModule[h[init : {__String}], body_] := 
 DynamicModule[{}, body, Initialization :> ToExpression[init]]

DynamicModule[h[{"a0 = 1.", "a1 = 1"}], {a0, a1, a1 + 3}]

{1., 1, 4}


dM[init : {__String}, body_, opts : OptionsPattern[DynamicModule]] := 
 DynamicModule[{}, body, Initialization :> ToExpression[init], opts]

dM[{"a0=1.", "a1=1"}, {a0, a1, a1 + 5}]

{1., 1, 6}


The definition is optimal open for the data type function taking a list of strings as input and giving a list of strings as output. The programming paradigms of Mathematica allow this to be maximal on the built-in-functions.

An option is to make use of the function from FeynCalc package Datatype to add some more mathematical meaningful function attributes.

Conducting a search in the Mathematica documentation for a list of strings give 54 pages of 10 results. The most modern change in Mathematica is the introduction of the Association. That makes the type of list of strings given somehow outdated. The association is a list of strings not packing the pairing inside the string but making it a list of key-value pairs. Your keys in this sense are a0 and a1. Your values are 1. and 1. Mathematica definition and examples are found at Association.

The Mathematica page for DynamicModule presents the {x,y,...} as symbols. Ths symbols can be assigned initial values. For the symbols, the implicit, built-in automation for data types is applied. So as m_goldberg stated, Your use is in many perspectives not very close to a good question style. But internally this equal sign is already interpreted in most cases an Association.

So in the case of string manipulation x="text" is appropriate. In the Neat Example section, even an empty list is presented by Wolfram.

Nice built-in functions are InputForm, TextString or ToExpression for conversion.

Attributes[DynamicModule] displays just the attributes of DynamicModule. But that are following the Mathematica documentation the attributes of the internal objects too.

You have to get clear for Yourself, that ToExpression is not an attribute. Hold has the attribute HoldAll. In the output, cell Hold is displayed to remind that this constant or variable is of type HoldAll. This state is removed a given in the documentation page for Hold with the operation ReleaseHold.

DynamicModule in the simplest form operates differently to the structure implied by Your question. It uses local variables and is able to give a result as output.

DynamicModule[{a = 3, b = 2, c = 1}, {a, b, c}.x^Range[3]]

is a fully-fledged example showing both a,b and c as local variables with initialization and x the variable. This defines a polynomial that can be operated on in the coefficient form without normalization and a constant term. It is not interactive at all but can be operated further. All output variables are reals in the Mathematica definition. So machine precision type numbers.

Dynamic and DynamicModule are in the plain definition prepared for direct presentation in a notebook. There has to be given variable name to the input to work on with that.

These two are related to the built-in-function Interpretation. In the context of Interpretation Your question gets a different intent.

represents an object that displays as e, but is interpreted as the unevaluated form of expr if supplied as input. 

This allows concepts like calculating with letter or text type or graphics type placeholders. Given initial values to Dynamic or DynamicModule.

A deeper insight is offered by the tutorial "Generalized input". The intent is too to create new input templates for interaction in expressions, graphics

  • $\begingroup$ How does this answer the question? This neither shows how to do what is asked in the question, nor does it present an alternative way of doing things (unless I am missing something of course) $\endgroup$ – Lukas Lang Mar 8 '20 at 14:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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