Created
June 18, 2015 18:13
-
-
Save paultcochrane/e6e5d383dbe59852741a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> | |
<html> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=UTF-8"> | |
<title>~/perl6-examples/categories/99-problems/P01-scottp.pl.html</title> | |
<meta name="Generator" content="Vim/7.4"> | |
<meta name="plugin-version" content="vim7.4_v1"> | |
<meta name="syntax" content="none"> | |
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,prevent_copy="> | |
<meta name="colorscheme" content="none"> | |
<style type="text/css"> | |
<!-- | |
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #ffffff; } | |
body { font-family: monospace; color: #000000; background-color: #ffffff; } | |
* { font-size: 1em; } | |
--> | |
</style> | |
<script type='text/javascript'> | |
<!-- | |
--> | |
</script> | |
</head> | |
<body> | |
<pre id='vimCodeElement'> | |
use v6; | |
=begin pod | |
=TITLE P01 - Find the last box of a list. | |
=AUTHOR Scott Penrose | |
=head1 LISP | |
P01 (*) Find the last box of a list. | |
Example: | |
* (my-last '(a b c d)) | |
(D) | |
Note that, in LISP-speak, the last "box" is the last one-element sublist of | |
the list. In perl6, a single element can generally be used as a list and | |
vice versa; as a result, this example does not distinguish between a single | |
element and a list containing a single element. | |
=head1 Example: | |
> say my_last <a b c d>; | |
d | |
=end pod | |
# a. One line example: | |
# <> can be used to generate an array, similar to perl 5 - qw<a b c d> | |
# [] is used to select the element number | |
# * means the number of elements | |
# say is like print to stdout with a new line | |
# .say can be called as everything is an object | |
<A B C D E F>[* - 1].say; | |
# b. Subroutine example | |
# @l lists can be passed in as parameters - no need to use references | |
# .elems - is the number of elements, this time called on the object | |
# say called in procedure form | |
sub my_last(@l) { | |
return @l[@l.elems - 1]; | |
} | |
say my_last(<A B C D>); | |
# c. Pop like perl5 | |
# pop the last element off, which also returns it | |
# say either way | |
say <X Y Z>.list.pop; | |
<X Y Z>.list.pop.say; | |
# vim: expandtab shiftwidth=4 ft=perl6 | |
</pre> | |
</body> | |
</html> | |
<!-- vim: set foldmethod=manual : --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment