solve k = if k == 0 then 0 else extract . dropWhile (notSameOddity k') . dropWhile (notIncludes k') . enhance $ [0..] where k' = abs k sums = scanl (+) 0 [1..] enhance = zip3 (cycle [0, 1, 1, 0]) sums first (x, _, _) = x second (_, x, _) = x third (_, _, x) = x notIncludes k = (< k) . second notSameOddity k = (/= k `rem` 2) . first extract = third . head assertEq x y = if x == y then return () else error $ "Assertion violated: first argument is " ++ show x ++ " but second argument is " ++ show y assertEq' = uncurry assertEq main = do assertEq' (solve 2, 3) assertEq' (solve 12, 7) putStrLn "Tests passed"