P01 - 查找列表的最后一个元素。
作者:Scott Penrose
LISP
P01 (*) Find the last box of a list. Example: * (my-last '(a b c d)) (D)
请注意,在 LISP 语法中,最后一个“元素”是列表中最后一个仅包含一个元素的子列表。在 perl6 中,单个元素通常可以用作列表,反之亦然;因此,本示例不区分单个元素和包含单个元素的列表。
示例
> say my_last <a b c d>; d
源代码: P01-scottp.pl
use v6; # 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>.Array.pop; <X Y Z>.Array.pop.say;