Skip to content

Instantly share code, notes, and snippets.

@cmditch
Last active May 23, 2019 02:10
Show Gist options
  • Save cmditch/890eb1498421be8a3ea5a955999e2f70 to your computer and use it in GitHub Desktop.
Save cmditch/890eb1498421be8a3ea5a955999e2f70 to your computer and use it in GitHub Desktop.
What I have so far... but doesn't quite work
module Codewars.Kata.SqIntoSq where
import Debug.Trace
decompose :: Integer -> Maybe [Integer]
decompose num = decomposer (num^2) (num-1)
decomposer :: Integer -> Integer -> Maybe [Integer]
decomposer total num =
case (num, squareSum list == total) of
(_, True) -> Just list
(0, _) -> Nothing
(num', False) -> decomposer total (num' - 1)
where
list = trace (show list') $ list' -- debug help
list' = foldr reducer [] [1..num]
squareSum = sum . fmap (^2)
reducer n acc =
if squareSum acc > total then
reducer n (tail acc)
else
n : acc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment