Permalink
Newer
Older
100644 30 lines (21 sloc) 641 Bytes
moritz
Jan 25, 2009
1
use v6;
2
3
use Test;
4
5
=begin desc
6
7
Closure over parameters of outer subs, as per
8
# L<S04/The Relationship of Blocks and Declarations/"Every block is a
9
# closure">
10
# L<S06/Dynamically scoped subroutines>
11
12
=end desc
13
14
plan 4;
15
16
sub factorial (Int $n) {
17
my sub facti (Int $acc, Int $i) {
18
return $acc if $i > $n;
19
facti($acc * $i, $i + 1);
20
}
21
facti(1, 1);
22
} ;
23
24
is factorial(0), 1, "closing over params of outer subs (0)";
25
is factorial(1), 1, "closing over params of outer subs (1)";
26
is factorial(2), 2, "closing over params of outer subs (2)";
27
is factorial(3), 6, "closing over params of outer subs (3)";
28
29
# vim: ft=perl6