Categories
Posts in this category
- A shiny perl6.org site
- Creating an entry point for newcomers
- Sprixel, a 6 compiler powered by JavaScript
- Another perl6.org iteration
- Blackjack and Perl 6
- Why I commit Crud to the Perl 6 Test Suite
- Custom operators in Rakudo
- Defined Behaviour with Undefined Values
- Dissecting the "Starry obfu"
- Perl 6: Failing Softly with Unthrown Exceptions
- The first Perl 6 module on CPAN
- Google Summer of Code Mentor Recap
- Building a Huffman Tree With Rakudo
- Immutable Sigils and Context
- Is Perl 6 really Perl?
- Perl 6: Lost in Wonderland
- Lots of momentum in the Perl 6 community
- Musing and the future of feather and the Pugs repository
- Musings on Rakudo's spectest chart
- My first executable from Perl 6
- Trying to implement new operators - failed
- Let's build an object
- Perl 6 is optimized for fun
- How to get a parse tree for a Perl 6 Program
- Perl 6 in 2009
- Perl 6 ticket life cycle
- The Perl 6 Advent Calendar
- How to Plot a Segment of a Circle with SVG
- Publicity for Perl 6
- Rakudo architectural overview
- Rakudo Rocks
- Rakudo "star" announced
- Rakudo's rough edges
- Rats and other pets
- Releasing Rakudo made easy
- Set Phasers to Stun!
- Starry Perl 6 obfu
- Recent Perl 6 Developments August 2008
- Strings and Buffers
- Subroutines vs. Methods - Differences and Commonalities
- A SVG plotting adventure
- A Syntax Highlighter for Perl 6
- Test Suite Reorganization: How to move tests
- The Happiness of Design Convergence
- Perl 6 Tidings from September and October 2008
- Perl 6 Tidings for November 2008
- Perl 6 Tidings from December 2008
- Perl 6 Tidings from January 2009
- Perl 6 Tidings from February 2009
- Perl 6 Tidings from March 2009
- Perl 6 Tidings from April 2009
- Perl 6 Tidings from May 2009
- Perl 6 Tidings from May 2009 (second iteration)
- Perl 6 Tidings from June 2009
- Perl 6 Tidings from August 2009
- Perl 6 Tidings from October 2009
- Timeline for a syntax change in Perl 6
- Visualizing match trees
- We write a Perl 6 book for you
- When we reach 100% we did something wrong
- Where Rakudo Lives Now
- Why was the Perl 6 Advent Calendar such a Success?
- What you can write in Perl 6 today
- Why you don't need the Y combinator in Perl 6
Mon, 04 May 2009
Blackjack and Perl 6
Permanent link
When you play Blackjack, you need to collect cards so that the sum of their values gets as close as possible to 21, but not above.
The tricky thing is that an Ace can count both as 1 or 11, whatever suits better for you. So how do we find the best value of a dealt set of cards?
Well, somhow we have to sum over all combinations. For example if you have
an ace and a three, you could represent them as 3 and
[1, 11] in your program. What you want as a result is the array
[4, 14].
The cross meta-operator lets us apply a + pairwise on all
possible combinations of a list (shown on Rakudo's REPL):
> say (1, 11 X+ 3).perl [4, 14]
In principle some of the values could have been greater than 21 (if you had two aces), so let's filter out only those that are at most as large as 21, and find the maximum:
> say [max] (1, 11 X+ 3).grep({$_ <= 21})
14
max is an operator that gives us the larger of two values, and
the reduction operator [...] applies this pair wise to all list
items.
Now that works, but you can have more than two cards on your hand, so we need to generalize it a bit..
In a perfect Perl 6 world we could simply write
[max] ([X+] @cards).grep: {$_ <= 21};, but that requires a quite
complicated thing named slice context, which Rakudo doesn't
implement yet.
So we have to work around the non-working [X+] by doing the
reduction manually:
> my @cards = [1, 11], 4, [1, 11];
say [max] @cards.reduce({ @($^a) X+ @($^b) }).grep: { $_ <= 21 }
16
(Note: splitted on two lines for readability, but should really be on one line.)
The reduce method does (roughly) the same as the equally named
sub in
Perl 5's List::Util
module: it calls the block with two arguments, where the first one is the
previous return value from the block (or the first array item on the first
call), and the second is the next list item. The difference is that in Perl 5
the arguments are stored in the special variables $a and
$b (about which strict.pm doesn't complain), whereas
in Perl 6 they are passed as ordinary arguments to the block. The
^ twigil (secondary sigil) specifies that the value of that
variable should be taken from the parameter list, in lexicographic order of
all such variables in the block.
Since X+ operates on lists and not on array references, the
arguments needs to be derefences. In Perl 5 you'd write that as
@{...}, in Perl 6 it's @(...) - with the slight
difference that the derefencing on a number is not an error, but simply
returns a list of that number.
(you can write the same thing a bit simpler with junctions, but that doesn't demonstrate the meta operators, and is discouraged for other reasons).
You see that you can still write scary code with Perl 6, and I hope you will play around with it a bit, join us on #perl6 and have the appropriate amount of fun!
Comments / Trackbacks:
Trackback URL:
/blog-en/perl-6/blackjack-and-perl-6.trackback
Write a comment
The comments on this blog post have been disabled; the comment form below will not work.