1
+ use v6 ;
2
+
1
3
= begin pod
2
4
3
- = head1 Problem
5
+ = head1 Balanced brackets
4
6
5
7
Generate a string with N opening brackets (“[”) and N closing brackets (“]”), in some arbitrary order.
6
8
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.
@@ -10,17 +12,19 @@ =head1 More
10
12
L < http://rosettacode.org/wiki/Balanced_brackets#Perl_6 >
11
13
12
14
= head1 What's interesting here?
13
- * idiomatic solutions
14
- * hyper operators
15
- * switch statement
16
- * roll
17
- * grammar
15
+
16
+ = item idiomatic solutions
17
+ = item hyper operators
18
+ = item switch statement
19
+ = item roll
20
+ = item grammar
18
21
19
22
= head2 Depth counter
20
23
21
24
= end pod
22
25
23
- sub balanced ($ s ) {
26
+ {
27
+ sub balanced ($ s ) {
24
28
my $ l = 0 ;
25
29
for $ s . comb {
26
30
when " ]" {
32
36
}
33
37
}
34
38
return $ l == 0 ;
35
- }
39
+ }
36
40
37
- my $ n = prompt " Number of brackets" ;
38
- my $ s = (<[ ] > xx $ n ). pick (* ). join ;
39
- say " $ s { balanced($ s ) ?? " is" !! " is not" } well-balanced"
41
+ my $ n = prompt " Number of brackets >" ;
42
+ my $ s = (<[ ] > xx $ n ). pick (* ). join ;
43
+ say " $ s { balanced($ s ) ?? " is" !! " is not" } well-balanced" ;
44
+ }
40
45
41
46
= begin pod
42
47
43
48
= head2 FP oriented
44
49
45
50
= end pod
46
51
47
- sub balanced ($ s ) {
52
+ {
53
+ sub balanced ($ s ) {
48
54
. none < 0 and . [* - 1 ] == 0
49
- given [\+ ] ' \\ ' «leg « $ s . comb ;
50
- }
55
+ given [\+ ] ' \\ ' «leg « $ s . comb ;
56
+ }
51
57
52
- my $ n = prompt " Number of bracket pairs: " ;
53
- my $ s = <[ ] >. roll($ n * 2 ). join ;
54
- say " $ s { balanced($ s ) ?? " is" !! " is not" } well-balanced"
58
+ my $ n = prompt " Number of bracket pairs: " ;
59
+ my $ s = <[ ] >. roll($ n * 2 ). join ;
60
+ say " $ s { balanced($ s ) ?? " is" !! " is not" } well-balanced" ;
61
+ }
55
62
56
63
= begin pod
57
64
58
65
= head2 String munging
59
66
60
67
= end pod
61
68
62
- sub balanced ($ _ is copy ) {
63
- () while s :g /'[]' // ;
69
+ {
70
+ sub balanced ($ _ is copy ) {
71
+ s :g /'[]' // while m /'[]' /;
64
72
$ _ eq ' ' ;
65
- }
73
+ }
66
74
67
- my $ n = prompt " Number of bracket pairs: " ;
68
- my $ s = <[ ] >. roll($ n * 2 ). join ;
69
- say " $ s is" , ' not' xx not balanced($ s )), " well-balanced" ;
75
+ my $ n = prompt " Number of bracket pairs: " ;
76
+ my $ s = <[ ] >. roll($ n * 2 ). join ;
77
+ say " $ s is" , ' not' xx not balanced($ s ), " well-balanced" ;
78
+ }
70
79
71
80
= begin pod
72
81
73
82
= head2 Prasing with a grammar
74
83
75
84
= end pod
76
85
77
- grammar BalBrack {
86
+ {
87
+
88
+ grammar BalBrack {
78
89
token TOP { ^ <balanced >* $ };
79
90
token balanced { '[]' | '[' ~ ']' <balanced > }
91
+ }
92
+
93
+ my $ n = prompt " Number of bracket pairs: " ;
94
+ my $ s = <[ ] >. roll($ n * 2 ). join ;
95
+ say " $ s { BalBrack. parse($ s ) ?? " is" !! " is not" } well-balanced" ;
96
+
80
97
}
81
-
82
- my $ n = prompt " Number of bracket pairs: " ;
83
- my $ s = <[ ] >. roll($ n * 2 ). join ;
84
- say " $ s { BalBrack. parse($ s ) ?? " is" !! " is not" } well-balanced" ;
85
98
86
99
= begin pod
87
100
88
101
= head1 Features used
89
102
90
- C < roll > - L < http://perlcabal.org/syn/S32/Containers.html#roll >
91
- C < given > - L < http://perlcabal.org/syn/S04.html#Switch_statements >
92
- C < prompt > - L < http://perlcabal.org/syn/S32/IO.html#prompt >
93
- C < grammar > - L < http://perlcabal.org/syn/S05.html#Grammars >
103
+ = item C < roll > - L < http://perlcabal.org/syn/S32/Containers.html#roll >
104
+ = item C < given > - L < http://perlcabal.org/syn/S04.html#Switch_statements >
105
+ = item C < prompt > - L < http://perlcabal.org/syn/S32/IO.html#prompt >
106
+ = item C < grammar > - L < http://perlcabal.org/syn/S05.html#Grammars >
94
107
95
108
= end pod
96
109
0 commit comments