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..]]
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_iin l:longest' l
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 wherecol 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.
- 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)
Here's how it works 👇
And get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career. Real lessons from building production software. No bullshit.
"Man, love your simple writing! Yours is the only newsletter I open and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Ready to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization
Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.
Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️