Skip to content

Instantly share code, notes, and snippets.

@newton-migosi
Created December 1, 2020 16:41
Show Gist options
  • Save newton-migosi/c2daf7e55857fc36e7b0b443adcaae02 to your computer and use it in GitHub Desktop.
Save newton-migosi/c2daf7e55857fc36e7b0b443adcaae02 to your computer and use it in GitHub Desktop.
Find all the ways to add up to a number
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