Permalink
Browse files

Merge branch 'master' of github.com:perl6/perl6-examples

  • Loading branch information...
2 parents c7658f1 + f8abe2c commit 86ca52011e2244a815ce6bc08ee6b932562fcfa1 @jani jani committed Apr 21, 2012
@@ -1,17 +1,37 @@
-# http://rosettacode.org/wiki/100_doors#Perl_6
+use v6;
-# Problem: You have 100 doors in a row that are all initially closed. You make
-# 100 passes by the doors. The first time through, you visit every door and
-# toggle the door (if the door is closed, you open it; if it is open, you close
-# it). The second time you only visit every 2nd door (door #2, #4, #6, ...).
-# The third time, every 3rd door (door #3, #6, #9, ...), etc, until you only
-# visit the 100th door.
+=begin pod
+
+=head1 100 Doors
+
+You have 100 doors in a row that are all initially closed. You make 100 passes
+by the doors. The first time through, you visit every door and toggle the door
+(if the door is closed, you open it; if it is open, you close it). The second
+time you only visit every 2nd door (door #2, #4, #6, ...). The third time,
+every 3rd door (door #3, #6, #9, ...), etc, until you only visit the 100th
+door.
+
+=head1 Task
+
+What state are the doors in after the last pass? Which are open,
+which are closed?
+
+=head1 More
+
+L<http://rosettacode.org/wiki/100_doors#Perl_6>
+
+=end pod
-# Question: What state are the doors in after the last pass? Which are open,
-# which are closed?
say "Door $_ is open" for 1..10 X** 2;
-# More about X** - http://perlcabal.org/syn/S03.html#Cross_operators
-# vim: expandtab shiftwidth=4 ft=perl6:
+=begin pod
+
+=head1 Features used
+
+C<X**> - L<http://perlcabal.org/syn/S03.html#Cross_operators>
+
+=end pod
+
+# vim: expandtab shiftwidth=2 ft=perl6:
@@ -0,0 +1,6 @@
+This directory contains some good examples taken from rosettacode.org. We
+make and effort at picking examples that cover some useful or novel aspect
+of the Perl 6 language, and share some pointers about where to get more
+information about these features.
+
+If you find an instructive example on rosettacode.org, then share it here!
@@ -1,3 +1,8 @@
http://rosettacode.org/wiki/24_game#Perl_6 - a little grammar, casting (?), eval, prompt, roll
-http://rosettacode.org/wiki/24_game#Perl_6 [the second one] - ZIP, -> ... {, xx, ...,
+*http://rosettacode.org/wiki/24_game#Perl_6 [the second one] - ZIP, -> ... {, xx, ...,
http://rosettacode.org/wiki/Accumulator_factory#Perl_6 - returning a sub, is copy, $^var, static vars in subs
+http://rosettacode.org/wiki/Ackermann_function#Perl_6 - easy example of multi, ?? !!
+http://rosettacode.org/wiki/Arbitrary-precision_integers_(included)#Perl_6 - metaoperator, casting, {} in string,
+http://rosettacode.org/wiki/Balanced_brackets#Perl_6 - lot of ways, idiomatic solutions, roll, given, <<, s///, xx, is copy, prompt, {} in string, grammar (quite a lot)
+
+* something else should be here....
@@ -0,0 +1,97 @@
+=begin pod
+
+=head1 Problem
+
+Generate a string with N opening brackets (“[”) and N closing brackets (“]”), in some arbitrary order.
+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.
+
+=head1 More
+
+L<http://rosettacode.org/wiki/Balanced_brackets#Perl_6>
+
+=head1 What's interesting here?
+* idiomatic solutions
+* hyper operators
+* switch statement
+* roll
+* grammar
+
+=head2 Depth counter
+
+=end pod
+
+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).pick(*).join;
+say "$s {balanced($s) ?? "is" !! "is not"} well-balanced"
+
+=begin pod
+
+=head2 FP oriented
+
+=end pod
+
+sub balanced($s) {
+ .none < 0 and .[*-1] == 0
+ given [\+] '\\' «leg« $s.comb;
+}
+
+my $n = prompt "Number of bracket pairs: ";
+my $s = <[ ]>.roll($n*2).join;
+say "$s { balanced($s) ?? "is" !! "is not" } well-balanced"
+
+=begin pod
+
+=head2 String munging
+
+=end pod
+
+sub balanced($_ is copy) {
+ () while s:g/'[]'//;
+ $_ eq '';
+}
+
+my $n = prompt "Number of bracket pairs: ";
+my $s = <[ ]>.roll($n*2).join;
+say "$s is", ' not' xx not balanced($s)), " well-balanced";
+
+=begin pod
+
+=head2 Prasing with a grammar
+
+=end pod
+
+grammar BalBrack {
+ token TOP { ^ <balanced>* $ };
+ token balanced { '[]' | '[' ~ ']' <balanced> }
+}
+
+my $n = prompt "Number of bracket pairs: ";
+my $s = <[ ]>.roll($n*2).join;
+say "$s { BalBrack.parse($s) ?? "is" !! "is not" } well-balanced";
+
+=begin pod
+
+=head1 Features used
+
+C<roll> - L<http://perlcabal.org/syn/S32/Containers.html#roll>
+C<given> - L<http://perlcabal.org/syn/S04.html#Switch_statements>
+C<prompt> - L<http://perlcabal.org/syn/S32/IO.html#prompt>
+C<grammar> - L<http://perlcabal.org/syn/S05.html#Grammars>
+
+=end pod
+
+# vim: expandtab shiftwidth=2 ft=perl6:
@@ -0,0 +1,38 @@
+use v6;
+
+=begin pod
+
+=head1 Copy a string
+
+This task is about copying a string. Where it is relevant, distinguish
+between copying the contents of a string versus making an additional
+reference to an existing string.
+
+=head2 More
+
+L<http://rosettacode.org/wiki/Copy_a_string#Perl_6>
+
+=end pod
+
+# There is no special handling needed to copy a string.
+{
+ my $original = 'Hello.';
+ my $copy = $original;
+ say $copy; # prints "Hello."
+ $copy = 'Goodbye.';
+ say $copy; # prints "Goodbye."
+ say $original; # prints "Hello."
+}
+
+# You can also bind a new variable to an existing one so that each refers
+# to, and can modify the same string.
+{
+ my $original = 'Hello.';
+ my $bound := $original;
+ say $bound; # prints "Hello."
+ $bound = 'Goodbye.';
+ say $bound; # prints "Goodbye."
+ say $original; # prints "Goodbye."
+}
+
+# vim: expandtab shiftwidth=2 ft=perl6:
@@ -0,0 +1,27 @@
+=begin pod
+
+=head1 Create a two-dimensional array at runtime
+
+Get two integers from the user, then create a two-dimensional array where the
+two dimensions have the sizes given by those numbers, and which can be accessed
+in the most natural way possible. Write some element of that array, and then
+output that element. Finally destroy the array if not done by the language
+itself.
+
+=head1 More
+
+L<http://rosettacode.org/wiki/Create_a_two-dimensional_array_at_runtime#Perl_6>
+
+=end pod
+
+
+my ($major,$minor) = prompt("Dimensions? ").comb(/\d+/);
+
+my @array := [ for ^$major { [ for ^$minor {'@'} ] } ];
+
+@array[ pick 1, ^$major ][ pick 1, ^$minor ] = ' ';
+
+.say for @array;
+
+
+# vim: expandtab shiftwidth=2 ft=perl6:
@@ -1,25 +1,46 @@
-#=head1 Description
-# The Hailstone sequence of numbers can be generated from a starting positive integer, n by:
-# If n is 1 then the sequence ends.
-# If n is even then the next n of the sequence = n/2
-# If n is odd then the next n of the sequence = (3 * n) + 1
-# The (unproven), Collatz conjecture is that the hailstone sequence for any starting number always terminates.
-# Task Description:
-# Create a routine to generate the hailstone sequence for a number.
-# Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
-# Show the number less than 100,000 which has the longest hailstone sequence together with that sequences length.
-# (But don't show the actual sequence)!
-
-#=head1 Code
- sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
-
- my @h = hailstone(27);
- say "Length of hailstone(27) = {+@h}";
- say ~@h;
-
- my $m = 0 => 0;
- $m max= +hailstone($_) => $_ for 1..99_999;
- say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
-
-#=head2 More
-# http://rosettacode.org/wiki/Hailstone_sequence#Perl_6
+use v6;
+
+=begin pod
+
+=head1 Hailstone sequence
+
+The Hailstone sequence of numbers can be generated from a starting positive
+integer, n by:
+
+* If n is 1 then the sequence ends.
+* If n is even then the next n of the sequence = n/2
+* If n is odd then the next n of the sequence = (3 * n) + 1
+
+The (unproven), Collatz conjecture is that the hailstone sequence for any
+starting number always terminates.
+
+=head1 Task
+
+Create a routine to generate the hailstone sequence for a number.
+
+Use the routine to show that the hailstone sequence for the number 27 has 112
+elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
+
+Show the number less than 100,000 which has the longest hailstone sequence
+together with that sequences length.
+
+(But don't show the actual sequence)!
+
+=head1 More
+
+U<http://rosettacode.org/wiki/Hailstone_sequence#Perl_6>
+
+
+=end pod
+
+sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
+
+my @h = hailstone(27);
+say "Length of hailstone(27) = {+@h}";
+say ~@h;
+
+my $m max= +hailstone($_) => $_ for 1..99_999;
+say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
+
+
+# vim: expandtab shiftwidth=2 ft=perl6:
@@ -1,34 +0,0 @@
-# The Hailstone sequence of numbers can be generated from a starting positive
-# integer, n by:
-#
-# - If n is 1 then the sequence ends.
-# - If n is even then the next n of the sequence = n/2
-# - If n is odd then the next n of the sequence = (3 * n) + 1
-#
-# The (unproven), Collatz conjecture is that the hailstone sequence for any
-# starting number always terminates.
-#
-# Task Description:
-#
-# - Create a routine to generate the hailstone sequence for a number.
-# - Use the routine to show that the hailstone sequence for the number 27 has
-# 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
-# - Show the number less than 100,000 which has the longest hailstone sequence
-# together with that sequences length.
-# - (But don't show the actual sequence)!
-#
-# http://rosettacode.org/wiki/Hailstone_sequence#Perl_6
-
-use v6;
-
-sub hailstone($n) { $n, { $_ %% 2 ?? $_ div 2 !! $_ * 3 + 1 } ... 1 }
-
-my @h = hailstone(27);
-say "Length of hailstone(27) = {+@h}";
-say ~@h;
-
-my $m max= +hailstone($_) => $_ for 1..99_999;
-say "Max length $m.key() was found for hailstone($m.value()) for numbers < 100_000";
-
-
-# vim: expandtab shiftwidth=2 ft=perl6:
@@ -1,8 +1,19 @@
-# http://rosettacode.org/wiki/Last_Fridays#Perl_6
-#
-# Write a program or a script that returns the last Fridays of each month
-# of a given year. The year may be given through any simple input method
-# in your language (command line, std in, etc.).
+use v6;
+
+=begin pod
+
+=head1 Last fridays of the year
+
+Write a program or a script that returns the last Fridays of each month
+of a given year. The year may be given through any simple input method
+in your language (command line, std in, etc.).
+
+=head1 More
+
+L<http://rosettacode.org/wiki/Last_Fridays#Perl_6>
+
+=end pod
+
sub MAIN (Int $year = Date.today.year) {
my @fri;
@@ -12,7 +23,15 @@ (Int $year = Date.today.year)
.say for @fri[1..12];
}
-# The MAIN sub: http://perlcabal.org/syn/S06.html#Declaring_a_MAIN_subroutine
-# Date objects: http://perlcabal.org/syn/S32/Temporal.html#Date
-#
-# vim: expandtab shiftwidth=4 ft=perl6:
+
+=begin pod
+
+=head1 Features used
+
+The MAIN sub - L<http://perlcabal.org/syn/S06.html#Declaring_a_MAIN_subroutine>
+
+Date objects - L<http://perlcabal.org/syn/S32/Temporal.html#Date>
+
+=end pod
+
+# vim: expandtab shiftwidth=2 ft=perl6:
Oops, something went wrong. Retry.

0 comments on commit 86ca520

Please sign in to comment.