Two sum: Difference between revisions

→‎{{header|Haskell}}: Alternative approach, using concatMap and nubBy
No edit summary
(→‎{{header|Haskell}}: Alternative approach, using concatMap and nubBy)
Line 309:
[1,3]
</pre>
 
Or, using concatMap and nubBy:
 
<lang haskell>import Data.List (elem, elemIndices, nubBy)
import Control.Monad (join)
 
summingPairIndices :: Int -> [Int] -> [[Int]]
summingPairIndices n xs =
(join . (flip elemIndices xs <$>)) <$>
nubBy
(\[x, y] zs -> and (flip elem zs <$> [x, y]))
(concatMap
(\[x, y] ->
if x == y
then []
else [ [x, y]
| x + y == n ]) $
sequence [xs, xs])
 
main :: IO ()
main = print (summingPairIndices 21 [0, 2, 11, 19, 90])</lang>
{{Out}}
<pre>[[1,3]]</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
9,659

edits