StringTools - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Programming : Names and Strings : StringTools Package : English Text : StringTools/IsBalanced

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.

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 X, str; option `Copyright (c) 1990 by the University of Waterloo. All rights reserved.`; if 1 < nargs and args[2] = (':-deep') then if type(A,'{`module`, table, procedure, moduledefinition}') then op(1,sscanf(sprintf("%m",eval(A)),"%m")) else op(1,sscanf(sprintf("%m",A),"%m")) end if elif type(A,'rtable') then rtable(rtable_indfns(A),rtable_dims(A),A,rtable_options(A),(':-readonly') = false) 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&pi;i3.0&comma;i&equals;1..5&colon;

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

sCache(512,('permanent') = [0 = 0, infinity = undefined, Pi = 0, -infinity = undefined, 1/4*Pi = 1/2*2^(1/2), I = I*sinh(1), 1/6*Pi = 1/2, 1/2*Pi = 1, 1/3*Pi = 1/2*3^(1/2)])

(7)

IsBalanceds&comma;([&comma;)]

true

(8)

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

true

(9)

IsBalanced ewtheorem{thm}[lemma]{Theorem}&comma;[{&comma;]}

true

(10)

IsBalanced ewtheorem{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&alpha;&plus;cos&beta;&pi;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