|
1 | 1 | =begin pod :tag<perl6>
|
2 | 2 |
|
3 |
| -=TITLE Interacting with the system |
| 3 | +=TITLE Doing math with Perl 6 |
4 | 4 |
|
5 |
| -=SUBTITLE How to work with other applications and the operating system at large |
| 5 | +=SUBTITLE Different mathematical paradigms and how they are implemented in this language. |
6 | 6 |
|
7 |
| -=head1 Running external programs. |
| 7 | +=head1 Sets. |
8 | 8 |
|
9 |
| -TBD |
| 9 | +Perl 6 includes the L<Set> data type, as well as support for L<most set operations|/language/setbagmix#Set/Bag_Operators>. L<Union and intersection|https://en.wikipedia.org/wiki/Algebra_of_sets> are not only native operations, they use their I<natural> symbols, ∩ and ∪. For instace, this code would check the fundamental laws of the arithmetic of sets for a limited number of sets: |
10 | 10 |
|
11 |
| -=head1 Interacting with the system in an asynchronous way |
| 11 | +=begin code |
| 12 | +my @arbitrary-numbers = ^100; |
| 13 | +my \U = @arbitrary-numbers.Set; |
| 14 | +my \Ø = ().Set; |
12 | 15 |
|
13 |
| -TBD |
| 16 | +my @sets; |
| 17 | +
|
| 18 | +@sets.push: Set.new( @arbitrary-numbers.pick( @arbitrary-numbers.elems.rand)) for @arbitrary-numbers; |
| 19 | +
|
| 20 | +my (@union, @intersection); |
| 21 | +
|
| 22 | +for @sets -> $set { |
| 23 | + @union.push: $set ∩ $set === $set; |
| 24 | + @intersection.push: $set ∪ $set === $set; |
| 25 | +} |
| 26 | +
|
| 27 | +say "Idempotent union is ", so @union.all; |
| 28 | +# OUTPUT: «Idempotent union is True» |
| 29 | +say "Idempotent intersection is ", so @intersection.all; |
| 30 | +# OUTPUT: «Idempotent intersection is True» |
| 31 | +my (@universe, @empty-set, @id-universe, @id-empty); |
14 | 32 |
|
15 |
| -=head1 Using the native interface |
| 33 | +for @sets -> \A { |
| 34 | + @universe.push: A ∪ U === U; |
| 35 | + @id-universe.push: A ∩ U === A; |
| 36 | + @empty-set.push: A ∩ Ø === Ø; |
| 37 | + @id-empty.push: A ∪ Ø === A; |
| 38 | +} |
| 39 | +
|
| 40 | +say "Universe dominates ", so @universe.all; # OUTPUT: «Universe dominates True» |
| 41 | +say "Empty set dominates ", so @empty-set.all; # OUTPUT: «Empty set dominates True» |
| 42 | +
|
| 43 | +say "Identity with U ", so @id-universe.all; # OUTPUT: «Identity with U True» |
| 44 | +say "Identity with Ø ", so @id-empty.all; # OUTPUT: «Identity with Ø True» |
| 45 | +=end code |
| 46 | +
|
| 47 | +In this code, not only we check if the equalities in the algebra of sets hold, we also use, via L<sigilless variables|/language/variables#index-entry-\_(sigilless_variables)> and the Unicode form of the set operators, expressions that are as close as possible to the original form; C<A ∪ U === U>, for example, except for the use of the L<value identity operator <===>|/routine/===> is very close to the actual mathematical expression in the L<Wikipedia entry|https://en.wikipedia.org/wiki/Algebra_of_sets>. |
| 48 | +
|
| 49 | +=head1 Arithmetic. |
16 | 50 |
|
17 | 51 | TBD
|
18 | 52 |
|
19 |
| -=head1 Defining your own associative structures |
| 53 | +=head1 Sequences |
20 | 54 |
|
21 | 55 | TBD
|
22 | 56 |
|
| 57 | +=head1 Mathematical constants |
23 | 58 |
|
| 59 | +TBD |
24 | 60 |
|
25 | 61 |
|
26 | 62 | =end pod
|
|
0 commit comments