MapleTA - Maple Programming Help

Home : Support : Online Help : Education : MapleTA Package : MapleTA/ToString

MapleTA

 ToString
 convert an expression to a string and escape special characters

 Calling Sequence ToString( expr ) ToString( expr, sequence=true )

Parameters

 expr - anything sequence - truefalse (optional) default is false dotm - truefalse (optional) default is false

Description

 • The ToString command converts the given expression to a string that has some characters encoded.  The result is a string that is suitable for use by the parse command in order to get the given expression back. More importantly, the result is usable by the Maple T.A. built-in "maple" command used in writing question algorithms.
 • If the sequence option is set to true and the given expression is a sequence of more than one item, then a sequence of strings is returned. This sequence is suitable for use with the "switch" command in Maple T.A.
 • If the dotm option is set to true, then the output will not be human readable. However, the output will have more state information, such as assigned values of named procedures among other things.

Examples

This example shows a sample expression converted to a string and demonstrates the round-trip back through parse

 > $\mathrm{expr}≔\left[\left[1,2,3\right],"a string",\mathrm{X}\right]$
 ${\mathrm{expr}}{:=}\left[\left[{1}{,}{2}{,}{3}\right]{,}{"a string"}{,}{\mathrm{X}}\right]$ (1)
 > $s≔\mathrm{MapleTA}:-\mathrm{ToString}\left(\mathrm{expr}\right)$
 ${s}{:=}{"\left[\left[1, 2, 3\right], \x22a string\x22, \x24X\right]"}$ (2)
 > $\mathrm{expr2}≔\mathrm{parse}\left(s,'\mathrm{statement}'\right)$
 ${\mathrm{expr2}}{:=}\left[\left[{1}{,}{2}{,}{3}\right]{,}{"a string"}{,}{\mathrm{X}}\right]$ (3)
 > $\mathrm{evalb}\left(\mathrm{expr}=\mathrm{expr2}\right)$
 ${\mathrm{true}}$ (4)

This example shows the difference between a sequence of strings and a single string.

 > $\mathrm{MapleTA}:-\mathrm{ToString}\left(\left[1,2\right],\left[3,4\right],\left[5,6\right],'\mathrm{sequence}'=\mathrm{true}\right)$
 ${"\left[1, 2\right]"}{,}{"\left[3, 4\right]"}{,}{"\left[5, 6\right]"}$ (5)
 > $\mathrm{MapleTA}:-\mathrm{ToString}\left(\left[1,2\right],\left[3,4\right],\left[5,6\right],'\mathrm{sequence}'=\mathrm{false}\right)$
 ${"\left[1, 2\right], \left[3, 4\right], \left[5, 6\right]"}$ (6)

This example shows that the value of p is not encoded in the non-dotm conversion

 > p := proc(x) x^2; end;
 ${p}{:=}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{^}{2}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (7)
 > $\mathrm{s1}≔\mathrm{MapleTA}:-\mathrm{ToString}\left(p\right)$
 ${\mathrm{s1}}{:=}{"p"}$ (8)
 > $\mathrm{s2}≔\mathrm{MapleTA}:-\mathrm{ToString}\left(p,'\mathrm{dotm}'=\mathrm{true}\right)$
 ${\mathrm{s2}}{:=}{"MapleTA:-Builtin:-maple\left(StringTools:-SubstituteAll\left(\x22op\left(sscanf\left(&dquote:I\\&dquote:pf*6#%\\&dquote:xG6\\&dquote:6\\&dquote:F&*\x249\x24\\&dquote:\\&dquote:#F&F&F&F&&dquote:,&dquote:%m&dquote:\right)\right)\x22, &dquote:, \x22\right)\right)"}$ (9)

Note that s1 only encoded p, while s2 has more information.  Reassign p so we can see what happens when the s2 statement is parsed and executed.

 > p := proc(x) x^3; end;
 ${p}{:=}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{^}{3}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (10)
 > $p\left(3\right)$
 ${27}$ (11)
 > $\mathrm{parse}\left(\mathrm{s2},'\mathrm{statement}'\right)$
 ${p}$ (12)

After parsing, the old value of p is restored

 > $p\left(3\right)$
 ${9}$ (13)

Some magic must be done to work around double-quotes inside strings

 > $s≔\mathrm{MapleTA}:-\mathrm{ToString}\left(\mathrm{HEIGHT}\left("6\text{'}2" tall"\right)\right)$
 ${s}{:=}{"MapleTA:-Builtin:-maple\left(StringTools:-SubstituteAll\left(\x22HEIGHT\left(&dquote:6\text{'}2\\&dquote: tall&dquote:\right)\x22, &dquote:, \x22\right)\right)"}$ (14)
 > $\mathrm{parse}\left(s,\mathrm{statement}\right)$
 ${\mathrm{HEIGHT}}{}\left({"6\text{'}2" tall"}\right)$ (15)

Compatibility

 • The MapleTA[ToString] command was introduced in Maple 18.