Skip to content

Instantly share code, notes, and snippets.

@paultcochrane
Created June 18, 2015 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paultcochrane/e6e5d383dbe59852741a to your computer and use it in GitHub Desktop.
Save paultcochrane/e6e5d383dbe59852741a to your computer and use it in GitHub Desktop.
<!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 &quot;box&quot; 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:
&gt; say my_last &lt;a b c d&gt;;
d
=end pod
# a. One line example:
# &lt;&gt; can be used to generate an array, similar to perl 5 - qw&lt;a b c d&gt;
# [] 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
&lt;A B C D E F&gt;[* - 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(&lt;A B C D&gt;);
# c. Pop like perl5
# pop the last element off, which also returns it
# say either way
say &lt;X Y Z&gt;.list.pop;
&lt;X Y Z&gt;.list.pop.say;
# vim&#0058; 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