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

Parameters

Description

Examples

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 ({ 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.

Examples

withStringTools:

IsBalancedf(x),(,)

true

(1)

IsBalancedf(x,g(s,t)),(,)

true

(2)

Y(define y (lambda (f) (let ((g (lambda (h) (lambda (x) ((f (h h)) x))))) (g g)))):

IsBalancedY,(,)

true

(3)

IsBalanced((call/cc call/cc) (call/cc call/cc)),(,)

true

(4)

ssprintf%a,evalcopy,1

sproc (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

(5)

IsBalanceds&comma;(&comma;)

true

(6)

seqsinπi3.0&comma;i=1..5&colon;

ssprintf%a&comma;op4&comma;evalsin&comma;1

sCache(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])

(7)

IsBalanceds&comma;([&comma;)]

true

(8)

IsBalancedsprintf%a&comma;evalsin&comma;1&comma;[{(&comma;]})

true

(9)

IsBalanced\newtheorem{thm}[lemma]{Theorem}&comma;[{&comma;]}

true

(10)

IsBalanced\newtheorem{thm}[lemma]{Theorem}&comma;[{&comma;}]

false

(11)

IsBalanced<math><apply><plus/><apply><times/><cn>2</cn><ci>a</ci></apply><ci>b</ci></apply></math>&comma;<&comma;>

true

(12)

mmlMathML:-Exportsinα+cosβπ10&colon;

IsBalancedmml&comma;&<&comma;;>

true

(13)

IsBalancedab[cd]ef&comma;[&comma;]

true

(14)

IsBalancedab]cd[ef&comma;[&comma;]

false

(15)

IsBalancedab]cd[ef&comma;]&comma;[

true

(16)

IsBalanceda[b[cd]ef&comma;[&comma;]

false

(17)

IsBalanceda[b[cd]]]]]ef&comma;[&comma;]

false

(18)

IsBalanceda[[[b&comma;(&comma;)

true

(19)

IsBalancedabcde&comma;a&comma;e

true

(20)

See Also

string

StringTools