# Perl Weekly Challenge 13

So I know I've fallen off the blogging about the weekly challenge. This is not because I've not been doing (I have) or I've not been enoying them (I also have). It's because my blogging platform (a neat little Perl based system called Statocles) kind of died while parsing the Markdown on a previous blog post and this got me wound up.

It also got me to restart my personal plan to make a similar system for building simple, fast to update, static sites using Perl6. The first part of which (that is mostly so I can learn Grammars) is a Pure Perl6 Markdown parser.

It's taking a while but I'm hoping in my upcoming week off I'll get a lot more of it done.

Anyway I just read Laurent Rosenfield's post about this week and I wanted to share one of my two solutions becuase I thought it was kind of neat.

## Hofstadter Female and Male sequences

So the Hofstadter Female and Male sequences are defined by the following equation :

``````F(0) = 1 ; M(0) = 0
F(n) = n - M(F(n-1)), n > 0
M(n) = n - F(M(n-1)), n > 0
``````

So it's a pair of recursive functions. Where to calculate the value for `n` in either sequnces you need to calulate the ones before in both sequences. The problem with doing it as a pair of recursive functions is that without some kind of caching you're going to be calculating the same values over and over again (especailly) the smaller values of `n`. Laurent covered some nice ways to cache the data but when I had a look the word sequence jumped out at me. Hence my final code looks like this :

``````sub MAIN ( UInt() \$n ) {
my @M;
my @F = lazy gather {
my \$n = 0;
take 1;
loop {
\$n++;
take \$n - @M[@F[\$n-1]];
}
}

@M = lazy gather {
my \$n = 0;
take 0;
loop {
\$n++;
take \$n - @F[@M[\$n-1]];
}
};

say "F : {@F[0..\$n].join(", ")}";
say "M : {@M[0..\$n].join(", ")}";
}
``````

Here I define a couple of lazy sequences that refer to one another. Note I have to predefine `@M` before I do `@F` or it raises and error but it's fine with my updating what `@M` is after the fact. I added some notes in the code to note before a `take` happened and each value is only taken once.

Later today I may run some speed tests of this code against the code Laurent wrote to see how performant it is.

## An aside

While doing the first challenge for this week I found a bug in Perl6 where is you create a `Range` or `Sequence` of `date` objects and you give the first a custom formatter after the 28th of the month the formatted drops off. This bug was fixed as soon as I raised it in the Perl6 IRC and will hopefully be resolved in the next release.

Which is pretty cool.

Anyway, more later. Hopefully.