Balanced brackets: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 598:
input is: []line 1:2 missing NEWLINE at ']'
</pre>
 
 
=={{header|AppleScript}}==
Line 1,791 ⟶ 1,790:
]]]] false
</lang>
 
 
=={{header|Common Lisp}}==
Line 1,839 ⟶ 1,837:
NIL: ]][[[[][]][[[[]]]]
</pre>
 
=={{header|Component Pascal}}==
BlackBox Component Builder
Line 1,913 ⟶ 1,912:
[]][[]:> $FALSE
</pre>
 
=={{header|D}}==
===Standard Version===
Line 3,792:
The string '[[]]][][[][]' is not OK. Unbalanced.
The string '][[][[][][]]][[]' is not OK. Unbalanced.
 
 
=={{header|Lua}}==
Line 4,461 ⟶ 4,460:
return !$depth
}</lang>
 
=={{header|Perl 6}}==
There's More Than One Way To Do It.
===Depth counter===
 
{{works with|Rakudo|2015.12}}
 
<lang perl6>sub balanced($s) {
my $l = 0;
for $s.comb {
when "]" {
--$l;
return False if $l < 0;
}
when "[" {
++$l;
}
}
return $l == 0;
}
 
my $n = prompt "Number of brackets";
my $s = (<[ ]> xx $n).flat.pick(*).join;
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</lang>
 
===FP oriented===
Here's a more idiomatic solution using a hyperoperator to compare all the characters to a backslash (which is between the brackets in ASCII), a triangle reduction to return the running sum, a <tt>given</tt> to make that list the topic, and then a topicalized junction and a topicalized subscript to test the criteria for balance.
<lang perl6>sub balanced($s) {
.none < 0 and .[*-1] == 0
given ([\+] '\\' «leg« $s.comb).cache;
}
 
my $n = prompt "Number of bracket pairs: ";
my $s = <[ ]>.roll($n*2).join;
say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"</lang>
 
===String munging===
Of course, a Perl 5 programmer might just remove as many inner balanced pairs as possible and then see what's left.
{{works with|Rakudo|2015.12}}
<lang perl6>sub balanced($_ is copy) {
Nil while s:g/'[]'//;
$_ eq '';
}
 
my $n = prompt "Number of bracket pairs: ";
my $s = <[ ]>.roll($n*2).join;
say "$s is", ' not' x not balanced($s), " well-balanced";</lang>
 
===Parsing with a grammar===
{{works with|Rakudo|2015.12}}
<lang perl6>grammar BalBrack { token TOP { '[' <TOP>* ']' } }
 
my $n = prompt "Number of bracket pairs: ";
my $s = ('[' xx $n, ']' xx $n).flat.pick(*).join;
say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";</lang>
 
=={{header|Phix}}==
Line 5,117 ⟶ 5,061:
 
</lang>
 
 
=={{header|R}}==
Line 5,176 ⟶ 5,119:
(for ([n 10]) (try n))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
There's More Than One Way To Do It.
===Depth counter===
 
{{works with|Rakudo|2015.12}}
 
<lang perl6>sub balanced($s) {
my $l = 0;
for $s.comb {
when "]" {
--$l;
return False if $l < 0;
}
when "[" {
++$l;
}
}
return $l == 0;
}
 
my $n = prompt "Number of brackets";
my $s = (<[ ]> xx $n).flat.pick(*).join;
say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"</lang>
 
===FP oriented===
Here's a more idiomatic solution using a hyperoperator to compare all the characters to a backslash (which is between the brackets in ASCII), a triangle reduction to return the running sum, a <tt>given</tt> to make that list the topic, and then a topicalized junction and a topicalized subscript to test the criteria for balance.
<lang perl6>sub balanced($s) {
.none < 0 and .[*-1] == 0
given ([\+] '\\' «leg« $s.comb).cache;
}
 
my $n = prompt "Number of bracket pairs: ";
my $s = <[ ]>.roll($n*2).join;
say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"</lang>
 
===String munging===
Of course, a Perl 5 programmer might just remove as many inner balanced pairs as possible and then see what's left.
{{works with|Rakudo|2015.12}}
<lang perl6>sub balanced($_ is copy) {
Nil while s:g/'[]'//;
$_ eq '';
}
 
my $n = prompt "Number of bracket pairs: ";
my $s = <[ ]>.roll($n*2).join;
say "$s is", ' not' x not balanced($s), " well-balanced";</lang>
 
===Parsing with a grammar===
{{works with|Rakudo|2015.12}}
<lang perl6>grammar BalBrack { token TOP { '[' <TOP>* ']' } }
 
my $n = prompt "Number of bracket pairs: ";
my $s = ('[' xx $n, ']' xx $n).flat.pick(*).join;
say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";</lang>
 
=={{header|Red}}==
Line 6,271 ⟶ 6,270:
balanced : x[ y [ [] z ]][ 1 ][]abcd
</pre>
 
=={{header|Simula}}==
<lang simula>BEGIN
Line 6,801:
'[[][]]': true,
'[[[]]]': true></pre>
 
=={{header|VBA}}==
<lang vb>
Line 7,233 ⟶ 7,234:
[[][]] OK
</pre>
 
 
=={{header|Ya}}==
10,333

edits