Skip to content

Instantly share code, notes, and snippets.

@philipnilsson
Created July 24, 2013 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipnilsson/6069238 to your computer and use it in GitHub Desktop.
Save philipnilsson/6069238 to your computer and use it in GitHub Desktop.
nonEmptySubsequences = foldr comb []
where comb x xs = xs ++ fmap (x:) ([]:xs)
findSum p = filter (p.sum) . nonEmptySubsequences
hasSum p = not.null . findSum p
-- Trying it in the REPL
hasSum (==0) [-7, 4, 3, 8, -5]
>> True
hasSum (==0) [-6, 3, 2]
>> False
hasSum (==13) [-7, 4, 3, 8, -5]
>> False
findSum (==0) [-7, 4, 3, 8, -5]
>> [[-7,4,8,-5],[-7,4,3]]
findSum (==13) [-7, 4, 3, 8, -5]
>> []
findSum odd [-7, 4, 3]
>> [[3],[4,3],[-7],[-7,4]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment