-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path00_all.p6
207 lines (127 loc) · 3.76 KB
/
00_all.p6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use v6;
=begin explanation
For our convenience (for now), all the demo programs are in this single file.
They will be broken out into separate files by a Perl 5 utility script.
This file is best edited with Vim sized to 80 columns X 25 lines. To size:
perl6 -e '.say for (^8 Xx 10).join, (^10).join x 8, 3..25;'
`scroll` should auto-set to 25; Ctrl-U and Ctrl-D to navigate.
Code+output *must* fit in a single 80x25 screen.
format:
#--- title Marks the start of a new, separate program
# Comment-only lines for new concepts
say some_code(); # brief explanation
say some_other_code(); # brief explanation
say $even_more + $code; # brief explanation
=begin output
output of some_code
output of some_other_code
output of addition
=end output
=end explanation
#--- say
# Statements end with ';', and comments start with '#'
# To output to the screen, use the say() or print() function.
say(4); say(32); # say() adds a "newline" to its output.
print(9); print(8); # print() does not add a newline.
say; # say() with no argument only outputs a newline.
say 7, 6; # The parenthesis are optional.
# Simple string literals are enclosed in single quotes.
say 'Hello, world!';
=begin output
4
32
98
76
Hello, world!
=end output
#--- new file
# XXX Stop being boring!
# Basic math operators have the same precedence as in Algebra.
# The ** operator is used for exponents ( X ** Y means X to the Yth power).
say 2 + 3 * 4 ** 2;
say 2 + 3 * 4 ** (1/2);
# The ~ operator concatenates (stitches together) two strings.
say 'sol' ~ 'ace';
# When you use a string where a number is expected, it gets converted to a
# number.
say '38' + '4';
# The reverse conversion also happens as needed.
say 2 ~ 'cool' ~ ( 3 + 1 ) ~ 'school';
=begin output
50
8
solace
42
2cool4school
=end output
#--- x and xx
say 'WOOT!' x 15; # The x operator "repeats" a string
say 2 + ( '9' x 3 );
say ( 'Marco!', 'Polo!' ) xx 3; # The xx operator repeats a list
=begin output
WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!WOOT!
1001
Marco! Polo! Marco! Polo! Marco! Polo!
=end output
#--- quotes - single and double
say "abc\n\txyz"; # Tabs and newlines can be used in double-quoted strings.
say 'abc\n\txyz'; # Single-quoted strings treat nothing as special.
=begin output
abc
xyz
abc\n\txyz
=end output
#--- Vars - scalar
# Variables are declared with "my".
# Simple variable names start with $
my $name;
$name = 'Camelia'; # Values are assigned to variables with =
# You can declare, calculate, and assign at the same time.
my $full_name = $name ~ ' the Butterfly';
say $full_name;
=begin output
Camelia the Butterfly
=end output
#--- control structures
# Comparison operators are either for numbers, or for strings
# Numeric: < > <= >= == !=
# String: lt gt le gt eq ne
# The basic conditionals: if, unless
my $age = 42;
if $age >= 17 {
say "You are of-age in Harry Potter's world";
}
my $mood = 'angry';
unless $mood eq 'happy' {
say 'No soup for you!';
}
=begin output
You are of-age in Harry Potter's world
No soup for you!
=end output
#--- Ranges1
say ~ ( 1 .. 4 ); # The .. operator creates a "range"
say ~ ( 'a' .. 'g' ); # Ranges automagically work with letters.
say ~ ( 'A' .. 'D', 'F' ); # A range can be part of a list
# Put ^ on either side (or both sides) of .. to exclude the endpoint.
say ~ ( 3 .. 6 );
say ~ ( 3 ^.. 6 );
say ~ ( 3 ..^ 6 );
say ~ ( 3 ^..^ 6 );
# A range is stored as only endpoints.
# The stringify op (~) forced the ranges to expand; say() does not.
say 3 ^..^ 6 ;
=begin output
1 2 3 4
a b c d e f g
A B C D F
3 4 5 6
4 5 6
3 4 5
4 5
3^..^6
=end output
# Local variables:
# c-file-style: "parrot"
# End:
# vim: expandtab shiftwidth=4 scroll=25 :