title: Getting array/list element index
body: <p>I was trying to solve <a href="https://leetcode.com/problems/two-sum/" rel="nofollow">this</a> little problem from LeetCode:</p>
  
  <pre><code>-module(two_sum).
  
  -export([main/2]).
  
  -spec main(List :: list(), Target :: non_neg_integer()) -&gt; list().
  
  %%%==================================================================
  %%% Export
  %%%==================================================================
  main(List, Target) -&gt;
    P = [{string:str(List, [X]), string:str(List, [Y]), X + Y} || X &lt;- List, Y &lt;- List, Y &gt; X andalso X + Y =:= Target],
    io:format("Result: ~w~n", [P]).
  %%[X + Y || X &lt;- List, Y &lt;- List, Y &gt; X].
  %%iterate(List, Target, 0, {}).
  
  %%%==================================================================
  %%% Internal
  %%%==================================================================
  iterate([], _Target, _Sum, _Result) -&gt; {};
  iterate([H | T], Target, Sum, Result) -&gt;
    %%io:format("H &gt;&gt; ~w; T &gt;&gt; ~w, Target &gt;&gt; ~w~n", [H, T, Target]).
    Acc = Sum + H,
    case Acc =:= Target of
      true -&gt; erlang:append_element(Result, H);
      false -&gt; iterate(T, Target, Acc, Result)
    end.
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Is there a more efficient way to get the element's index from an array/list? Currently I'm using this: <code>{string:str(List, [X])</code>, but I'm not sure if that's correct, although it gets the job done.</li>
  <li>If I happen to get, for instance, a result like this: <code>R = [{1,2,9},{1,3,13},{1,4,17},{2,3,18},{2,4,22},{3,4,26}]</code>, how do I pattern match for <code>{1,2,9}</code>, knowing that <code>9</code> is <code>Target</code>? I tried <code>[{X1, X2, Target}] = R</code>...but it didn't like it!</li>
  </ol>