After an awesome longboarding session yesterday afternoon I decided to play around with infinite sequences in Haskell – it’s supposed to be one of the more (most?) powerful features of Haskell – because it’s a lazy language apparently.

My first impulse of creating a primes generator was nipped in the bud by a long page of prime number generators in Haskell. Scary, complex, mindboggling.

Project Euler posed a much better challenge: *Which starting number, under one million, produces the longest collatz chain?*

The solution I came up with was a simple brute force generator of infinitely many collatz sequences. Then I would take the first 1,000,000 find the maximum and that’s that.

collatz :: Integer -> [Integer] collatz 1 = [] collatz n | odd n = 3*n+1:collatz(3*n+1) | even n = div n 2:collatz(div n 2) chains = [collatz x | x <- [1..]] |

Didn’t help.

So I started looking for the maximum a bit differently – take all the sequences, sort them by length and take the last one.

longest max = last $ sortBy (comparing snd) $ zip [1..] $ map length $ take max chains |

Great! It worked! But it takes ~26 seconds!

Well sorting maybe isn’t the best idea ever, so let’s try creating a list of sequences where the list’s tail only contains those sequences that are longer than the head. A sprinkle of dropWhile and it was done.

longest' (max_i, max_l) = let l = head $ dropWhile (\(i,l) -> l <= max_l) $ zip [max_i..] $ map length $ chains' max_i in l:longest' l |

~25 seconds!

That’s odd … even odder still is running both algorithms one after another only takes 33 seconds. Huh?

It would seem I’m using memoization incorrectly. I’ve heard it performs funny in recursive functions. The theory I formulated last night was that because haskell was lazy each execution chain was constructed to its end and the intermittent memoized values never got used until the whole function was called again.

Looking at the code samples this morning, though, I discovered this:

collatz :: Integer -> [Integer] collatz = memoize col where col 1 = [] col n | odd n = 3*n+1:collatz(3*n+1) | even n = div n 2:collatz(div n 2) |

As you can see, I don’t call the memoized function internally. Just goes to show what a night’s sleep can do to one’s coding abilities. I bashed my head against this problem for four hours yesterday and I never noticed I was recursing to the wrong function!

Interestingly enough, fixing that makes the algorithm spaz out and die after 16 seconds. The only output I get is “Killed”. Curious.

###### Related articles

- Graphing the Collatz Conjecture (mrhonner.com)
- The Point of Laziness in Programing Languages (existentialtype.wordpress.com)
- Learning me a Haskell (swizec.com)
- Memoization explained using a python implementation of fibonacci (geeklogs.posterous.com)

PS: You should also follow me on twitter 👉 here.

It's where I go to shoot the shit about programming.