IsBalanced - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

# Online Help

###### All Products    Maple    MapleSim

StringTools

 IsBalanced
 determine if fences in a string are balanced

 Calling Sequence IsBalanced( s, left, right )

Parameters

 s - string; string to test left - character; left fence character right - character; right fence character

Description

 • The IsBalanced(s,left,right) command checks whether the string s is balanced with respect to fence characters left and right. The characters left and right must be distinct, but are otherwise unrestricted.
 • Typical fence characters include, but are not limited to, braces ($"\left\{"$ and $"\right\}"$), parentheses ($"\left("$ and $"\right)"$), brackets ($"\left["$ and $"\right]"$) and scare quotes ($"`"$ and $"\text{'}"$).
 • The string s is deemed balanced with respect to the given fence characters if each open fence in left is matched by a subsequent, corresponding close fence in right within s. Note that it is not sufficient that corresponding fences be equal in number in s.
 • If s is the empty string, or left and right are empty strings, the command returns true.
 • All of the StringTools package commands treat strings as (null-terminated) sequences of $8$-bit (ASCII) characters.  Thus, there is no support for multibyte character encodings, such as unicode encodings.

Examples

 > $\mathrm{with}\left(\mathrm{StringTools}\right):$
 > $\mathrm{IsBalanced}\left("f\left(x\right)","\left(","\right)"\right)$
 ${\mathrm{true}}$ (1)
 > $\mathrm{IsBalanced}\left("f\left(x,g\left(s,t\right)\right)","\left(","\right)"\right)$
 ${\mathrm{true}}$ (2)
 > $Y≔"\left(define y \left(lambda \left(f\right) \left(let \left(\left(g \left(lambda \left(h\right) \left(lambda \left(x\right) \left(\left(f \left(h h\right)\right) x\right)\right)\right)\right)\right) \left(g g\right)\right)\right)\right)":$
 > $\mathrm{IsBalanced}\left(Y,"\left(","\right)"\right)$
 ${\mathrm{true}}$ (3)
 > $\mathrm{IsBalanced}\left("\left(\left(call/cc call/cc\right) \left(call/cc call/cc\right)\right)","\left(","\right)"\right)$
 ${\mathrm{true}}$ (4)
 > $s≔\mathrm{sprintf}\left("%a",\mathrm{eval}\left(\mathrm{copy},1\right)\right)$
 (5)
 > $\mathrm{IsBalanced}\left(s,"\left(","\right)"\right)$
 ${\mathrm{true}}$ (6)
 > $\mathrm{seq}\left(\mathrm{sin}\left(\frac{\mathrm{Pi}i}{3.0}\right),i=1..5\right):$
 > $s≔\mathrm{sprintf}\left("%a",\mathrm{op}\left(4,\mathrm{eval}\left(\mathrm{sin},1\right)\right)\right)$
 ${s}{≔}{"Cache\left(512,\left(\text{'}permanent\text{'}\right) = \left[0 = 0, 1/2*Pi = 1, 1/6*Pi = 1/2, 1/4*Pi = 1/2*2^\left(1/2\right), 1/3*Pi = 1/2*3^\left(1/2\right), -infinity = undefined, Pi = 0, infinity = undefined, I = I*sinh\left(1\right)\right]\right)"}$ (7)
 > $\mathrm{IsBalanced}\left(s,"\left(\left[","\right)\right]"\right)$
 ${\mathrm{true}}$ (8)
 > $\mathrm{IsBalanced}\left(\mathrm{sprintf}\left("%a",\mathrm{eval}\left(\mathrm{sin},1\right)\right),"\left[\left\{\left(","\right]\right\}\right)"\right)$
 ${\mathrm{true}}$ (9)
 > $\mathrm{IsBalanced}\left("ewtheorem\left\{thm\right\}\left[lemma\right]\left\{Theorem\right\}","\left[\left\{","\right]\right\}"\right)$
 ${\mathrm{true}}$ (10)
 > $\mathrm{IsBalanced}\left("ewtheorem\left\{thm\right\}\left[lemma\right]\left\{Theorem\right\}","\left[\left\{","\right\}\right]"\right)$
 ${\mathrm{false}}$ (11)
 > $\mathrm{IsBalanced}\left("\left[itex\right]2ab\left[/itex\right]","<",">"\right)$
 ${\mathrm{true}}$ (12)
 > $\mathrm{mml}≔\mathrm{MathML}:-\mathrm{Export}\left({\left(\mathrm{sin}\left(\mathrm{α}\right)+\mathrm{cos}\left(\mathrm{β}\right)-\mathrm{Pi}\right)}^{10}\right):$
 > $\mathrm{IsBalanced}\left(\mathrm{mml},"&<",";>"\right)$
 ${\mathrm{true}}$ (13)
 > $\mathrm{IsBalanced}\left("ab\left[cd\right]ef","\left[","\right]"\right)$
 ${\mathrm{true}}$ (14)
 > $\mathrm{IsBalanced}\left("ab\right]cd\left[ef","\left[","\right]"\right)$
 ${\mathrm{false}}$ (15)
 > $\mathrm{IsBalanced}\left("ab\right]cd\left[ef","\right]","\left["\right)$
 ${\mathrm{true}}$ (16)
 > $\mathrm{IsBalanced}\left("a\left[b\left[cd\right]ef","\left[","\right]"\right)$
 ${\mathrm{false}}$ (17)
 > $\mathrm{IsBalanced}\left("a\left[b\left[cd\right]\right]\right]\right]\right]ef","\left[","\right]"\right)$
 ${\mathrm{false}}$ (18)
 > $\mathrm{IsBalanced}\left("a\left[\left[\left[b","\left(","\right)"\right)$
 ${\mathrm{true}}$ (19)
 > $\mathrm{IsBalanced}\left("abcde","a","e"\right)$
 ${\mathrm{true}}$ (20)

 See Also