Two sum: Difference between revisions
Content added Content deleted
(→Haskell – Returning all matches: (Perhaps a little cleaner in terms of >>= ?)) |
(→Haskell Returning all matches: Dropped imports, simplified in terms of >>=, adjusted type signature) |
||
Line 314: | Line 314: | ||
====Returning all matches==== |
====Returning all matches==== |
||
Listing multiple solutions when they exist: |
|||
<lang haskell> |
<lang haskell>sumTo :: Int -> [Int] -> [(Int, Int)] |
||
sumTo n ns = |
|||
let ixs = zip [0 ..] ns |
|||
summingPairIndices :: Int -> [Int] -> [[Int]] |
|||
in ixs >>= |
|||
summingPairIndices n xs = |
|||
(\(i, x) -> |
|||
⚫ | |||
nubBy |
|||
\(j, y) -> |
|||
[ (i, j) |
|||
( |
| (i < j) && (x + y) == n ]) |
||
⚫ | |||
| x /= y && x + y == n ])) |
|||
main :: IO () |
main :: IO () |
||
main = print $ |
main = print $ sumTo 21 [0, 2, 11, 19, 90, 10]</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>[ |
<pre>[(1,3),(2,5)]</pre> |
||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |