1 ! Copyright (c) 2012 Anonymous
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel math sequences ;
4 IN: rosetta-code.balanced-brackets
6 ! https://rosettacode.org/wiki/Balanced_brackets
10 ! Generate a string with N opening brackets (“[”) and N closing
11 ! brackets (“]”), in some arbitrary order.
13 ! Determine whether the generated string is balanced; that is,
14 ! whether it consists entirely of pairs of opening/closing
15 ! brackets (in that order), none of which mis-nest.
22 ! [[][]] OK []][[] NOT OK
24 :: balanced? ( str -- ? )
32 } case counter + counter!
33 counter 0 < [ f ok! ] when
35 ok [ counter 0 <= ] [ f ] if ;