Created
December 1, 2020 16:41
-
-
Save newton-migosi/c2daf7e55857fc36e7b0b443adcaae02 to your computer and use it in GitHub Desktop.
Find all the ways to add up to a number
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sumTo target n pool = loop n 0 [] | |
where | |
loop 0 runningTotal accumulator = do | |
guard (runningTotal == target) | |
return accumulator | |
loop counter runningTotal accumulator = do | |
current <- pool | |
case accumulator of | |
[] -> guard True | |
(previous:_) -> guard (current < previous) | |
guard (current + runningTotal <= target) | |
loop (counter-1) (runningTotal+current) (current:accumulator) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment