Solving a (surprisingly difficult) puzzle with functional programming in Scheme