4
4
5
5
= SUBTITLE Learning Perl 6 from Haskell, in a nutshell: What do I already know?
6
6
7
- Haskell and Perl6 are I < very > different languages. This is obvious. However, that
7
+ Haskell and Perl 6 are I < very > different languages. This is obvious. However, that
8
8
does not mean there are not similarities or shared ideas! This page attempts to
9
- get a Haskell user up and running with Perl6 . The Haskell user may find that they need
10
- not abandon all of their Haskelly thoughts while scripting in Perl6 .
9
+ get a Haskell user up and running with Perl 6 . The Haskell user may find that they need
10
+ not abandon all of their Haskelly thoughts while scripting in Perl 6 .
11
11
12
12
Note that this should not be mistaken for a beginner tutorial or overview of Perl
13
13
6; it is intended as a technical reference for Perl 6 learners with a strong
@@ -32,7 +32,7 @@ You do not mix types and values in Haskell like the below
32
32
plusTwo Integer -- This is not valid
33
33
= end code
34
34
35
- In Perl6 , types (AKA type objects) live on the same level as values
35
+ In Perl 6 , types (AKA type objects) live on the same level as values
36
36
37
37
= begin code
38
38
sub plus-two(Int $x --> Int) { $x + 2 }
@@ -41,7 +41,7 @@ In Perl6, types (AKA type objects) live on the same level as values
41
41
plus-two(Int); # This is valid
42
42
= end code
43
43
44
- I will illustrate this unique aspect of Perl6 with one more example:
44
+ I will illustrate this unique aspect of Perl 6 with one more example:
45
45
46
46
= begin code
47
47
multi sub is-string(Str $ --> True) {}
@@ -64,7 +64,7 @@ Let's say you have a hypothetical function that parses a String to an Integer:
64
64
Nothing -> 0
65
65
= end code
66
66
67
- In Perl6 , since type objects coexist with regular objects, we have the concept of Defined
67
+ In Perl 6 , since type objects coexist with regular objects, we have the concept of Defined
68
68
and Undefined objects. Plain type objects are undefined while instantiated objects are defined.
69
69
70
70
= begin code
@@ -77,7 +77,7 @@ and Undefined objects. Plain type objects are undefined while instantiated objec
77
77
}
78
78
= end code
79
79
80
- So in Perl6 we have type constraints that indicate the definedness of a type. These are
80
+ So in Perl 6 we have type constraints that indicate the definedness of a type. These are
81
81
82
82
= begin code
83
83
Int:D; # This is a defined Int.
@@ -116,7 +116,7 @@ The C<with> operator that you see above is like C<if>, except it explicitly test
116
116
passes the result to the following block. Similarly, C < without > tests that the object is undefined and also
117
117
passes the result to the following block.
118
118
119
- For more natural control flow with undefined and defined types, Perl6 introduces C < andthen > and C < orelse > .
119
+ For more natural control flow with undefined and defined types, Perl 6 introduces C < andthen > and C < orelse > .
120
120
121
121
= begin code
122
122
sub parse-int(Str $s --> Int:_) { ... }
@@ -130,11 +130,11 @@ For more natural control flow with undefined and defined types, Perl6 introduces
130
130
131
131
TODO: include a better example for andthen that makes sense. Maybe using promise objects?
132
132
133
- So in practice, Perl6 does not have the concept of a null type, but rather of defined or undefined types.
133
+ So in practice, Perl 6 does not have the concept of a null type, but rather of defined or undefined types.
134
134
135
135
= head2 Data Definitions
136
136
137
- Perl6 is fundamentally an Object Oriented language. However, it also gives you the freedom
137
+ Perl 6 is fundamentally an Object Oriented language. However, it also gives you the freedom
138
138
to write in virtually any paradigm you wish. If you only want to pure functions that take an
139
139
object and return a new object, you can certainly do so.
140
140
@@ -147,7 +147,7 @@ Here is a Haskell code example:
147
147
moveUp (Point x y) = Point x (y + 1)
148
148
= end code
149
149
150
- And an equivalent Perl6 example:
150
+ And an equivalent Perl 6 example:
151
151
152
152
= begin code
153
153
class Point { has $.x; has $.y; }
@@ -158,7 +158,7 @@ And an equivalent Perl6 example:
158
158
= end code
159
159
160
160
The code I illustrated above is an example of a Product Type. If instead you'd like to
161
- write a Sum Type, there is not an exact equivalent in Perl6 . The closest thing would be
161
+ write a Sum Type, there is not an exact equivalent in Perl 6 . The closest thing would be
162
162
an Enum.
163
163
164
164
= begin code :skip-test
@@ -169,7 +169,7 @@ an Enum.
169
169
testAnimal Horse = "Neigh"
170
170
= end code
171
171
172
- A Perl6 Enum does not fit the same exact use cases, but it can be used in putting
172
+ A Perl 6 Enum does not fit the same exact use cases, but it can be used in putting
173
173
constraints on types.
174
174
175
175
= begin code
@@ -195,15 +195,15 @@ existing types.
195
195
fullName first last = first ++ last
196
196
= end code
197
197
198
- The equivalent in Perl6 is the following.
198
+ The equivalent in Perl 6 is the following.
199
199
200
200
= begin code
201
201
my constant Name = Str;
202
202
203
203
sub full-name ( Name \first, Name \last --> Name ) { first ~ last }
204
204
= end code
205
205
206
- It should be noted that in Perl6 , one can also create a subset of an existing type.
206
+ It should be noted that in Perl 6 , one can also create a subset of an existing type.
207
207
208
208
= begin code :skip-test
209
209
subset Name of Str where *.chars < 20;
@@ -220,7 +220,7 @@ It should be noted that in Perl6, one can also create a subset of an existing ty
220
220
221
221
TODO
222
222
223
- explain how Perl6 roles compare to Haskell typeclasses
223
+ explain how Perl 6 roles compare to Haskell typeclasses
224
224
225
225
= head1 Functions
226
226
@@ -237,7 +237,7 @@ Haskell makes heavy use of pattern matching in function definitions.
237
237
greeting name = "Hello, " ++ name ++ "!"
238
238
= end code
239
239
240
- Perl6 does this as well! You just use the C < multi > keyword to signify that it is a multiple dispatch
240
+ Perl 6 does this as well! You just use the C < multi > keyword to signify that it is a multiple dispatch
241
241
function.
242
242
243
243
= begin code
@@ -257,7 +257,7 @@ more information in error messages, and for introspection.
257
257
say &greeting.signature; # (Str \name --> Str)
258
258
= end code
259
259
260
- An interesting thing to note in the Perl6 code above is that passing values like C < 'bub' > as a
260
+ An interesting thing to note in the Perl 6 code above is that passing values like C < 'bub' > as a
261
261
function parameter is just syntax sugar for a C < where > guard.
262
262
263
263
=Guards
@@ -341,7 +341,7 @@ Haskell makes heavy use of case matching like the below:
341
341
_ -> "don't care"
342
342
= end code
343
343
344
- In Perl6 you can achieve this same thing with the given/when structure:
344
+ In Perl 6 you can achieve this same thing with the given/when structure:
345
345
346
346
= begin code
347
347
my $number = {...};
@@ -367,11 +367,11 @@ data shapes become quite intuitive, but it takes a bit of practice.
367
367
368
368
= head2 List Comprehensions
369
369
370
- TODO compare haskell list comprehensions to Perl6 gather/take
370
+ TODO compare haskell list comprehensions to Perl 6 gather/take
371
371
372
372
= head2 Fold
373
373
374
- Fold in Haskell is called Reduce in Perl6 .
374
+ Fold in Haskell is called Reduce in Perl 6 .
375
375
376
376
= begin code :skip-test
377
377
mySum = foldl `+` 0 numList
@@ -383,7 +383,7 @@ Fold in Haskell is called Reduce in Perl6.
383
383
@numbers.reduce({$^a + $^b}, with => 0)
384
384
= end code
385
385
386
- However, in Perl6 , if you want to use an infix operator (+ - / % etc) there is a nice little
386
+ However, in Perl 6 , if you want to use an infix operator (+ - / % etc) there is a nice little
387
387
helper called the Reduction Metaoperator.
388
388
389
389
= begin code
@@ -394,7 +394,7 @@ helper called the Reduction Metaoperator.
394
394
395
395
It inserts the operator in between all values in the list and produces a result, just like Fold.
396
396
397
- In Haskell you, you have foldl and foldr. In Perl6 , this difference is determined by the
397
+ In Haskell you, you have foldl and foldr. In Perl 6 , this difference is determined by the
398
398
associativity attached to the operator/subroutine.
399
399
400
400
= begin code
426
426
427
427
= head2 Ranges
428
428
429
- Haskell and Perl6 both allow you to specify ranges of values.
429
+ Haskell and Perl 6 both allow you to specify ranges of values.
430
430
431
431
= begin code :skip-test
432
432
myRange1 = 10..100
@@ -442,7 +442,7 @@ Haskell and Perl6 both allow you to specify ranges of values.
442
442
443
443
= head2 Laziness vs Eagerness
444
444
445
- In the examples above, you have the concept of laziness displayed very plainly. Perl6 has laziness
445
+ In the examples above, you have the concept of laziness displayed very plainly. Perl 6 has laziness
446
446
only where it makes the most sense. For example, in the range 10..100, this is eager because it has
447
447
a definite end. If a list does not have a definite end, then the list should clearly be lazy.
448
448
@@ -451,7 +451,7 @@ a definite end. If a list does not have a definite end, then the list should cle
451
451
(1 .. Inf).is-lazy; # True
452
452
= end code
453
453
454
- These are the "sane defaults" that Perl6 takes pride in. But they are still defaults and can be
454
+ These are the "sane defaults" that Perl 6 takes pride in. But they are still defaults and can be
455
455
changed into one or the other.
456
456
457
457
= begin code
0 commit comments