Balanced brackets: Difference between revisions

(→‎{{header|Python}}: Task asks for a generator too!)
Line 9:
[[][]] OK []][[] NOT OK
 
=={{header|Perl 6D}}==
<lang d>import std.stdio, std.algorithm, std.string;
 
void main() {
foreach (s; ",[],[][],[[][]],][,][][,[]][[]".split(","))
writefln("%s is%s balanced", s,
s.balancedParens('[', ']') ? "" : " not");
}</lang>
Output:
<pre> is balanced
[] is balanced
[][] is balanced
[[][]] is balanced
][ is not balanced
][][ is not balanced
[]][[] is not balanced</pre>
=={{header|Perl 6}}==
<lang perl6>sub balanced($s) {
my $l = 0;
Line 28 ⟶ 43:
my $s = (<[ ]> xx $N).pick(*).join;
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>Procedure Balanced(String$)
Anonymous user