I won't claim to be particularly proud of this implementation as a function with an artiy of 5 is a huge smell to me but it works.

No, it wasn't TDD'd. I had the acceptance test at the beginning. I spent the rest of my time figuring out how to make it work in Erlang. I'd love to see idiomatic implementations that provide more clarity.