Saturday, June 26, 2010

Tail Recursion

There was a conversation at work a few weeks back on the difference between recursion and iteration. Someone made the claim "Recursion doesn't always work," and pointed out that the Fibonacci Sequence, while easy to implement in naive recursion, tends to blow up when the numbers get large.

I argued that there is an efficient solution using tail-recursion. It's faster than naive recursion, and simpler than an iterative solution.

A third person pointed out tail-recursion is a Scheme thing, and not all languages properly optimize it. This is completely true, but... it's also true that tail-recursive algorithms are in principle more efficient. Abelson and Sussman point out that doesn't always translate into actual performance, though:
most implementations of common languages (including Ada, Pascal, and C) are designed in such a way that the interpretation of any recursive procedure consumes an amount of memory that grows with the number of procedure calls, even when the process described is, in principle, iterative. As a consequence, these languages can describe iterative processes only by resorting to special-purpose ``looping constructs'' such as do, repeat, until, for, and while.

So here's a short test... I wrote a short tail-recursive Fib generator in Common Lisp. Note it takes a number (i.e. the number of terms to generate) and returns a list representing the sequence:

(defun fibonacci-sequence (num)
"Calculate a Fibonacci sequence to a number NUM."
(labels ((fib (n acc)
(cond ((equalp n 0) acc)
(T (fib (1- n)
(cons (+ (car acc)
(cadr acc)) acc))))))
(cond ((= num 0) '(1))
((= num 1) '(1 1))
((> 0 num) 'undefined)
(T (reverse (fib (- num 2) '(1 1)))))))

We can loosely translate that to Perl. Perl doesn't have an equivalent to Common Lisp's 'labels', so I had to write two named functions to implement it. But this short script is more-or-less equivalent to the Lisp version: it takes a number on the command line and prints a list representing a sequence with that number of terms:


=head1 NAME

=head1 AUTHOR

Clumsy Ox



Calculates the Fibonacci Sequence to $NUMBER terms.

This is just an exercise in tail-recursion.


use strict;
use warnings;

use Data::Dumper;

my $number = shift;

my @sequence = fibonacci ($number);

print STDOUT join (', ', @sequence), "\n";

=head2 fibonacci

fibonacci ($number) => @sequence

sub fibonacci {
my $num = shift;

return (1) if $num == 0;
return (1, 1) if $num == 1;

return reverse fibt( $num - 2, 1, 1);

=head2 fibt

fibt ($number) => @sequence

sub fibt {
my $num = shift;
return @_ if $num == 0;
return fibt ( $num - 1, $_[0] + $_[1], @_);

Notice both solutions accumulate the sequence as a list. So there is some definite overhead in carrying that sort of data structure, but it's "fair" in the sense that both are having to do it.

Just informally checking it, the Perl solution is slower than the Lisp solution, but not by an amazing amount. I ran a quick-n-dirty test of the Perl solution, and it timed out reasonably. But I found it blew Perl's number stack very quickly and went to 'inf':

bash-3.2$ time ./ 10000 > /tmp/output
Deep recursion on subroutine "main::fibt" at ./ line 56.

real 0m1.072s
user 0m0.640s
sys 0m0.380s

Just for comparison, Lisp returned:

(time (fib-sequence 10000))
Evaluation took:
0.019 seconds of real time
0.018860 seconds of total run time (0.012529 user, 0.006331 system)
[ Run times consist of 0.010 seconds GC time, and 0.009 seconds non-GC time. ]
100.00% CPU
47,236,537 processor cycles
4,893,824 bytes consed

Both took longer to print the result than to actually calculate it.

I haven't tried the experiment in Java or C, but I think it might be interesting to see what would happen.

Incidentally, according to SBCL, the 10,000th element of the Fibonacci Sequence is a 2090 digit number:

(format t "~:d" (car (last (fib-sequence 10000))))

Update: I decided to try a quick-n-dirty Java implementation. It's rough and ugly, but it seems to work.

Here's where it gets interesting: the Java solution works just fine, but it's slow, and it runs into a StackOverflow at just over 10,000 elements. I suspect that could be increased dramatically with some better java runtime settings, but it seems to disprove my thesis that tail recursion is an appropriate solution regardless of implementation language.

I was also able to get Perl to give me better results with
use bignum;
Still, Lisp is by far the fasest solution.

Thursday, June 24, 2010

Poor Navigation

So I'm in Raleigh, NC this week. I head back to the Northwest tomorrow afternoon. Since I'm so close, I lined up lunch with a good friend in Charlotte. It was what they call a "wild hair": a two-to-three hour drive for lunch, but I figured it might be worth it.

I have a pretty sorry little rental car this week, so between my fear of it dying on the Interstate and a desire for a nicer drive, I decided to take 64 to Asheboro, then 49 to Charlotte:

View Larger Map

But I missed the turn from Hwy 1 to Hwy 64 in Apex. I have no idea how I missed it, but I did. I started getting nervous when I saw signs for Southern Pines, but still hadn't seen signs for Siler City.

Yep, I ended up in Moore County, "on the way" to Charlotte. I was south of Sanford.

I had to call both people I meant to meet and tell them. They both laughed, so there were no hard feelings. But I sure felt stupid.


Friday, June 18, 2010

Grill pizza stone

In an effort to make the Redneck Pizza Oven a more repeatable experiment, I've been looking for a pizza stone designed for use on the grill. Then Ames bought me this one from Weber as a gift. I gave it a whirl yesterday.

The pies made on the stone bubbled nicely and looked actually rather picturesque:
From Grill Pizza Stone

From Grill Pizza Stone

But when we cut them, we realized the bottoms were charred to almost inedible:
From Grill Pizza Stone

It appears we've been able to get the stone good and hot, but the air above it is too cool. We had this problem last time, and ended up shovelling live coals around the perimeter of the stone to get that temperature up:

So it seems we need to make some arrangement like that.

The stone itself appears to be exactly what I was looking for: it handled the heat just fine, although I was half-afraid it would crack. It's marketed specifically for use on a grill, and I read the directions very carefully: I used it exactly in accordance with the directions in the box.

It doesn't look nearly so picturesque now that the stone's been a little charred and the metal's been a little blued, but I have to say it's extremely promising. I think this is going to become a centerpiece of our future pizza explorations.

From Grill Pizza Stone

From Grill Pizza Stone

Thursday, June 17, 2010

Happy Anniversary to us

So Ames and I have been married 15 years today.


Saturday, June 5, 2010

NC Bound

I'm heading out to Raleigh, North Carolina June 20--25. It's a business trip: I won't have a ton of time for hobnobbing, but I will absolutely make some time for what friends and family I can reasonably get in touch with.