
练习答案
|
447
1
3
7
There are 5 arguments
1
3
7
6
5
There are 4 arguments
1
3
7
(6 5)
There are 3 arguments
1
3
(7 (6 5))
6.
你已经知道如何建立一个库导出子例程。对于这两个函数,你需要使用一个
slurpy
参数来扁平化参数。
head
返回第一个参数,
tail
返回其他参数。这两个
子例程的签名是一样的,而且两者都使用
is export trait
,会在加载这个库的作
用域中定义这两个子例程:
# HeadsTails.pm
sub head ( *@args ) is export { return @args[0] }
sub tail ( *@args ) is export { return @args[1..*-1] }
你可能发现其实你不需要做这个工作,因为已经有现成的
.head
和
.tail
方法。
第
12
章练习答案
1.
在使用类之前要先定义。大括号里不需要有任何代码。尽管看起来为空,但应该
也能从这些类创建对象:
class Butterfly {}
class Moth {}
class Lobster {}
my $number = Butterfly.new;
my $str = Moth.new;
my $set = Lobster.new;
2.
这个程序与前一个练习中的程序很类似。不过不是定义这些类,这里要用
use