Equilibrium index: Difference between revisions

m
→‎{{header|Haskell}}: Tidied the Prelude functions version
(Updated the program to compile with Nim 1.4. Changed slightly the output. Added the output.)
m (→‎{{header|Haskell}}: Tidied the Prelude functions version)
Line 1,151:
<lang haskell>equilibriumIndices :: [Int] -> [Int]
equilibriumIndices xs =
(zip3
let matchIndex (x, y, i) a
(scanl1 (+) xs) -- |Sums xfrom ==the y = i : aleft
(scanr1 (+) xs) -- |Sums otherwisefrom =the aright
[0 ..] -- Indices
in foldr
>>= (\(x, y, i) -> [i | x == y])
matchIndex
[]
(zip3
(scanl1 (+) xs) -- Sums from the left
(scanr1 (+) xs) -- Sums from the right
[0 ..] -- Indices
)
 
--------------------------- TEST--- -------------------------
main :: IO ()
main =
mapM_
print
(equilibriumIndices <$> equilibriumIndices
<$> [ [-7, 1, 5, 2, -4, 3, 0],
, [2, 4, 6],
, [2, 9, 2],
, [1, -1, 1, -1, 1, -1, 1],
, [1],
, []
])</lang>
{{Out}}
<pre>[3,6]
9,655

edits