5
$\begingroup$

The documentation for StringTemplate has the following example

In[1]:= StringTemplate[
  "The first `1` Fibonnaci numbers are: <* Table[Fibonacci[n+1],{n,#1}] // Row *>."][5]

Out[1]= "The first 5 Fibonnaci numbers are: 1, 2, 3, 5, 8."

However, when used in Mathematica, we get the following output

In[31]:= StringTemplate[
  "The first `1` Fibonnaci numbers are: <* Table[Fibonacci[n+1],{n,#1}] // Row *>."][5]

Out[31]= "The first 5 Fibonnaci numbers are: 12358."

How can one get the output with the commas between the numbers (i.e., like 1, 2, 3, 5, 8)?

$\endgroup$

2 Answers 2

1
$\begingroup$

The documentation example is clearly wrong. Probably was written without proper testing. A second argument giving a spacer element must be added to Row. The quotes delimiting the spacer string must be escaped.

StringTemplate[
  "The first `1` Fibonnaci numbers are: <* \
     Row[Table[Fibonacci[n+1],{n, #1}],\", \"] *>."][5]
"The first 5 Fibonnaci numbers are: 1, 2, 3, 5, 8."
$\endgroup$
5
$\begingroup$

I would say that there may be a mistake in the documentation. In order to get the comma separated format, you should provide an explicit separator to Row:

StringTemplate["The first `1` Fibonacci numbers are: <* Row[Table[Fibonacci[n + 1], {n, #1}], \", \"] *>."][5]
$\endgroup$
0

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.