Skip to content

Instantly share code, notes, and snippets.

@pjrt
Last active December 4, 2015 17:03
Show Gist options
  • Save pjrt/ddb49b19904704c1a7c3 to your computer and use it in GitHub Desktop.
Save pjrt/ddb49b19904704c1a7c3 to your computer and use it in GitHub Desktop.
equi in Haskell
equi :: [Int] -> Maybe Int
equi [] = Nothing
equi ns = go 0 0 ns
where
fullSum = sum ns
go _ _ [] = Nothing
go n leftSum (x:xs) =
let rightSum = fullSum - leftSum - x
in if rightSum == leftSum
then Just n
else go (n + 1) (leftSum + x) xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment