Jump to content

Balanced brackets: Difference between revisions

Line 475:
script mf
on bracket(_)
if iff((random number) < 0.5, then"[", "]")
"["
else
"]"
end if
end bracket
end script
Line 492 ⟶ 488:
set lngChars to length of xs
if lngChars > 0 then
ifset iNext to iDepth + iff(item 1 of xs = "[", then1, -1)
set iNext to iDepth + 1
else
set iNext to iDepth - 1
end if
if iNext < 0 then -- closing bracket unmatched
Line 504 ⟶ 496:
errorIndex(items 2 thru -1 of xs, iNext, iIndex + 1)
else -- end of string
if iff(iNext = 0 then, -- balanced - no1, problemiIndex)
-1
else
iIndex -- position of problem
end if
end if
end if
else
if iff(iDepth = 0, -1, theniIndex)
-1
else
iIndex
end if
end if
end errorIndex
Line 539 ⟶ 523:
set blnOK to (i = -1)
ifset strStatus to iff(blnOK, "OK", then"problem")
set strStatus to "OK"
else
set strStatus to "problem"
end if
set strLine to "'" & s & "'" & ¬
(items (w + 2) thru -1 of strPad) & strStatus
if blnOK then
set strPointer to iff(blnOK, set"", strPointerlinefeed to& nreps(space, i + 1) & "^")
else
set strPointer to linefeed & nreps(space, i + 1) & "^"
end if
intercalate("", {strLine, strPointer})
end report
Line 611 ⟶ 589:
return o & s
end nreps
 
-- Value of one of two expressions
-- Bool -> a -> b -> c
on iff(bln, f, g)
if bln then
set e to "["f
else
set e to "]"g
end if
if class of e is handler then
mReturn(e)'s lambda()
else
end ife
end if
end iff
 
-- Lift 2nd class function into 1st class wrapper
Line 627 ⟶ 620:
property lambda : f
end script
end mClosure</lang>
</lang>
 
{{Out}}
Line 634 ⟶ 628:
'' OK
'[]' OK
'][[]]' OKproblem
^
'[]][][]]' problem
^
'[[]]]]]]][' problem
^
']]][][]][]' problem
^
'][[[[][[[]][]]' problem OK
'[[[]][][][]][]' problemOK
^</pre>
 
=={{header|AutoHotkey}}==
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.