StringTools
IsBalanced
determine if fences in a string are balanced
Calling Sequence
Parameters
Description
Examples
IsBalanced( s, left, right )
s
-
string; string to test
left
character; left fence character
right
character; right fence character
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 ({ and }), parentheses (( and )), brackets ([ and ]) and scare quotes (` and ').
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.
with⁡StringTools:
IsBalanced⁡f(x),(,)
true
IsBalanced⁡f(x,g(s,t)),(,)
Y≔(define y (lambda (f) (let ((g (lambda (h) (lambda (x) ((f (h h)) x))))) (g g)))):
IsBalanced⁡Y,(,)
IsBalanced⁡((call/cc call/cc) (call/cc call/cc)),(,)
s≔sprintf⁡%a,eval⁡copy,1
s≔proc (A) local B, G, H, X, str; option `Copyright (c) 1990 by the University of Waterloo. All rights reserved.`; if 1 < _npassed and _passed[2] = ':-deep' then if type(A,'{`module`, table, procedure, moduledefinition}') then op(1,sscanf(sprintf("%m",eval(A)),"%m")) else B := op(1,sscanf(sprintf("%m",A),"%m")); if B::':-rtable' and B::':-attributed' then G, H := selectremove(type,[attributes(B)],':-identical'(':-source_rtable') = ':-rtable'); if 0 < numelems(G) then setattribute(B,seq(H)) end if end if; return B end if elif type(A,'rtable') then B := rtable(rtable_indfns(A),rtable_dims(A),A,rtable_options(A),':-readonly' = ':-false'); if B::':-attributed' then G, H := selectremove(type,[attributes(B)],':-identical'(':-source_rtable') = ':-rtable'); if 0 < numelems(G) then setattribute(B,seq(H)) end if end if; return B elif type(A,'table') then if type(A,'array') then array(A) elif type(A,'cache') then Cache(A) else table(A) end if elif type(A,'procedure') then subs(X = X,eval(A)) elif type(A,'{`module`, moduledefinition}') then if type(A,'record') then Record(eval(A)) elif type(A,'object') then Object(A) else error "cannot copy a module or module definition" end if else A end if end proc
IsBalanced⁡s,(,)
seq⁡sin⁡π⁢i3.0,i=1..5:
s≔sprintf⁡%a,op⁡4,eval⁡sin,1
s≔Cache(512,'permanent' = [0 = 0, 1/3*Pi = 1/2*3^(1/2), -infinity = undefined, 1/6*Pi = 1/2, Pi = 0, infinity = undefined, I = I*sinh(1), 1/4*Pi = 1/2*2^(1/2), 1/2*Pi = 1])
IsBalanced⁡s,([,)]
IsBalanced⁡sprintf⁡%a,eval⁡sin,1,[{(,]})
IsBalanced⁡\newtheorem{thm}[lemma]{Theorem},[{,]}
IsBalanced⁡\newtheorem{thm}[lemma]{Theorem},[{,}]
false
IsBalanced⁡<math><apply><plus/><apply><times/><cn>2</cn><ci>a</ci></apply><ci>b</ci></apply></math>,<,>
mml≔MathML:-Export⁡sin⁡α+cos⁡β−π10:
IsBalanced⁡mml,&<,;>
IsBalanced⁡ab[cd]ef,[,]
IsBalanced⁡ab]cd[ef,[,]
IsBalanced⁡ab]cd[ef,],[
IsBalanced⁡a[b[cd]ef,[,]
IsBalanced⁡a[b[cd]]]]]ef,[,]
IsBalanced⁡a[[[b,(,)
IsBalanced⁡abcde,a,e
See Also
string
Download Help Document