Task:

 Generate a string with   N   opening brackets   [   and with   N   closing brackets   ],   in some arbitrary order. 
 Determine whether the generated string is balanced; that is, whether it consists entirely of pairs of opening/closing brackets (in that order), none of which mis-nest.


Examples

   (empty)      OK
   []           OK   
   [][]         OK   
   [[][]]       OK 
   ][         NOT OK
   ][][       NOT OK
   []][[]     NOT OK



go1 ?=>
  tests(Tests),
  member(Test,Tests),
  printf("%s: ", Test),
  (  balanced_brackets(Test) ->
       println("OK")
     ;
       println("NOT OK")
  ),
  fail,
  nl.
go1 => true.
 
% Check if a string of [] is balanced
balanced_brackets(B) => 
   C = 0,
   foreach(I in 1..B.length, C >= 0)
      C:= C + cond(B[I] = '[', 1, -1)
   end,
   C == 0.
 
tests(["","[]", "[][]", "[[][]]", "][",
       "][][", "[]][[]", "[][][][][][][][][][]",
       "[[[[[[[]]]]]]]", "[[[[[[[]]]]]]",
       "[][[]][]","[[][]][]", "[][][[]][]"]).