Two sum: Difference between revisions
Content added Content deleted
No edit summary |
(→{{header|Haskell}}: Alternative approach, using concatMap and nubBy) |
||
Line 309: | Line 309: | ||
[1,3] |
[1,3] |
||
</pre> |
</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}}== |
=={{header|Icon}} and {{header|Unicon}}== |