Longest segment of balanced parentheses – an exercise in program inversion in a segment problem (Functional Pearl)
Given a string of parentheses, the task is to find a longest consecutive segment that is properly bracketed. We find it an interesting problem because it involves two techniques: the usual approach for solving segment problems, and the converse-of-a-function theorem – through which we derived an instance of shift-reduce parsing.
READ FULL TEXT