-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtailcalls
executable file
·128 lines (103 loc) · 2.77 KB
/
tailcalls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env perl
# Copyright (c) 2015 Christian Jaeger, [email protected]
# This is free software. See the file COPYING.md that came bundled
# with this file.
# This file shows how to do optimized tail-calls in Perl; see point
# (4b) in the file `basics` for the version without optimized
# tail-calls.
# ------------------------------------------------------------------
use strict;
use warnings;
use warnings FATAL => 'uninitialized';
use Cwd 'abs_path';
our ($mydir, $myname);
BEGIN {
my $location = (-l $0) ? abs_path($0) : $0;
$location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
($mydir, $myname) = ($1, $2);
}
use lib "$mydir/../lib";
use Chj::Backtrace;
use FP::Repl;
# ------------------------------------------------------------------
# again, see (4b) in `basics` for basic explanations
sub functional_fact {
my ($x) = @_;
functional_fact_iter($x, 1)
}
our @functional_inspect;
sub functional_fact_iter {
my ($x, $res) = @_;
push @functional_inspect, sub { ($x, $res) };
if ($x < 2) {
return $res;
} else {
# This is a tail call. Instead of doing it unoptimized like:
#
# functional_fact_iter($x - 1, $x * $res)
#
# we're making use of Perl's goto &$subroutine feature (see
# `perldoc -f goto`):
@_ = ($x - 1, $x * $res);
goto \&functional_fact_iter
# Yes that's rather ugly; see the file `more_tailcalls`
# for a better-looking way.
}
}
# To really see the difference, here's a function that we can usefully
# test for higher numbers of iterations:
sub odd {
my ($n) = @_;
if ($n == 0) {
0
} else {
even($n - 1)
}
}
sub even {
my ($n) = @_;
if ($n == 0) {
1
} else {
odd($n - 1)
}
}
# $ ulimit -S -v 200000; ./tailcalls
# main> even 4
# $VAR1 = 1;
# main> even 5
# $VAR1 = 0;
# main> even 500
# Deep recursion on subroutine "main::even" at ./tailcalls line 65.
# Deep recursion on subroutine "main::odd" at ./tailcalls line 74.
# $VAR1 = 1;
# main> even 500000
# Deep recursion on subroutine "main::even" at ./tailcalls line 65.
# Deep recursion on subroutine "main::odd" at ./tailcalls line 74.
# Out of memory!
# You can see that Perl ran out of space for the stack.
sub opt_odd {
my ($n) = @_;
if ($n == 0) {
0
} else {
@_ = ($n - 1);
goto \&opt_even
}
}
sub opt_even {
my ($n) = @_;
if ($n == 0) {
1
} else {
@_ = ($n - 1);
goto \&opt_odd
}
}
# $ ulimit -S -v 200000; ./tailcalls
# main> opt_even 5000000
# $VAR1 = 1;
# Now it runs with little (and constant) memory usage.
# ------------------------------------------------------------------
# enter the repl for your experiments, see (0) in `basics`
repl;