http://www.perlmonks.org?node_id=590113

I recently stumbled across 99 Lisp Problems (which was originally 99 Prolog Problems). It looked like fun, so I started answering those problems in Perl 6. This has shown me that I don't know Perl 6 nearly as well as I would like. However, here are my first 9 answers:

#!/usr/bin/pugs use v6-alpha; #P01 (*) Find the last box of a list. # Example: # * (my-last '(a b c d)) # (D) <a b c d>[-1].say; #P02 (*) Find the last but one box of a list. # Example: # * (my-but-last '(a b c d)) # (C D) <a b c d>[-2, -1].perl.say; #P03 (*) Find the K'th element of a list. # The first element in the list is number 1. # Example: # * (element-at '(a b c d e) 3) # C <a b c d>[2].perl.say; #P04 (*) Find the number of elements of a list. <a b c d>.elems.say; #P05 (*) Reverse a list. <a b c d>.reverse.perl.say; #P06 (*) Find out whether a list is a palindrome. # A palindrome can be read forward or backward; e.g. (x a m a x). for [<a b c b a>], [<a b c b c a>] -> $array { if $array.reverse ~~ $array { say $array.perl ~ " is a palindrome"; } else { say $array.perl ~ " is not a palindrome"; } } #P07 (**) Flatten a nested list structure. # Transform a list, possibly holding lists as elements into a `flat +' list by # replacing each list with its elements (recursively). # courtesy of Wim Vanderbauwhede from the perl6-users list my $flatten = -> $x { $x.isa(Array) ?? ( map $flatten, $x ) !! $x }; my @flattened = map $flatten, ('a', ['b', ['c', 'd', 'e']]); @flattened.perl.say; #P08 (**) Eliminate consecutive duplicates of list elements. # If a list contains repeated elements they should be replaced with + a single # copy of the element. The order of the elements should not be chan +ged. # Example: # * (compress '(a a a a b c c a a d e e e e)) # (a b c a d E) my $compress = do { my $previous; $compress = sub ($x) { if $x ne $previous { $previous = $x; return $x; } else { return; } }; } my @compressed = map $compress, <a a a a b c c a a d e e e e>; @compressed.perl.say; #P09 (**) Pack consecutive duplicates of list elements into sublists. # If a list contains repeated elements they should be placed in sep +arate sublists. # # Example: # * (pack '(a a a a b c c a a d e e e e)) # ((A A A A) (B) (C C) (A A) (D) (E E E E)) my @packed; { my @sublist; my $previous; for <a a a a b c c a a d e e e e> -> $x { if $x eq $previous { @sublist.push($x); next; } $previous = $x; if @sublist { @packed.push([@sublist]); @sublist = $x; } } @packed.push([@sublist]); } @packed.perl.say;

Needless to say, I have a lot more work to do, but feel free to tackle them. You should probably read the latest Perl6 docs.

All of the above examples run under the latest version of Pugs.

Hint for problem 10: it's very, very close to problem 9.

Problem 10: Run-length encoding of a list.
Use the result of problem P09 to implement the so-called run-length encoding data compression method. Consecutive duplicates of elements are encoded as lists (N E) where N is the number of duplicates of the element E.

Example:
* (encode '(a a a a b c c a a d e e e e))
((4 A) (1 B) (2 C) (2 A) (1 D)(4 E))

Update: Swapped POD for comments to make it shorter and easier to read. Also, if you struggle with a problem, try writing it in Perl 5 first.

Cheers,
Ovid

New address of my CGI Course.