Balanced brackets: Difference between revisions

Content added Content deleted
Line 475: Line 475:
script mf
script mf
on bracket(_)
on bracket(_)
if (random number) < 0.5 then
iff((random number) < 0.5, "[", "]")
"["
else
"]"
end if
end bracket
end bracket
end script
end script
Line 492: Line 488:
set lngChars to length of xs
set lngChars to length of xs
if lngChars > 0 then
if lngChars > 0 then
if item 1 of xs = "[" then
set iNext to iDepth + iff(item 1 of xs = "[", 1, -1)
set iNext to iDepth + 1
else
set iNext to iDepth - 1
end if
if iNext < 0 then -- closing bracket unmatched
if iNext < 0 then -- closing bracket unmatched
Line 504: Line 496:
errorIndex(items 2 thru -1 of xs, iNext, iIndex + 1)
errorIndex(items 2 thru -1 of xs, iNext, iIndex + 1)
else -- end of string
else -- end of string
if iNext = 0 then -- balanced - no problem
iff(iNext = 0, -1, iIndex)
-1
else
iIndex -- position of problem
end if
end if
end if
end if
end if
else
else
if iDepth = 0 then
iff(iDepth = 0, -1, iIndex)
-1
else
iIndex
end if
end if
end if
end errorIndex
end errorIndex
Line 539: Line 523:
set blnOK to (i = -1)
set blnOK to (i = -1)
if blnOK then
set strStatus to iff(blnOK, "OK", "problem")
set strStatus to "OK"
else
set strStatus to "problem"
end if
set strLine to "'" & s & "'" & ¬
set strLine to "'" & s & "'" & ¬
(items (w + 2) thru -1 of strPad) & strStatus
(items (w + 2) thru -1 of strPad) & strStatus
if blnOK then
set strPointer to ""
set strPointer to iff(blnOK, "", linefeed & nreps(space, i + 1) & "^")
else
set strPointer to linefeed & nreps(space, i + 1) & "^"
end if
intercalate("", {strLine, strPointer})
intercalate("", {strLine, strPointer})
end report
end report
Line 611: Line 589:
return o & s
return o & s
end nreps
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
e
end if
end iff


-- Lift 2nd class function into 1st class wrapper
-- Lift 2nd class function into 1st class wrapper
Line 627: Line 620:
property lambda : f
property lambda : f
end script
end script
end mClosure</lang>
end mClosure
</lang>


{{Out}}
{{Out}}
Line 634: Line 628:
'' OK
'' OK
'[]' OK
'[]' OK
'[[]]' OK
'][[]' problem
^
'[]][][' problem
'[]]]]]' problem
^
^
'[[]]]]]]' problem
']]]]]]][' problem
^
']]][][]][]' problem
^
^
'][[[[][[[]][' problem
'[[][[[]]]]' OK
'[[[][][][]]]' OK
^</pre>
</pre>


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==