Task[edit]
Find the index of a string (needle) in an indexable, ordered collection of strings (haystack).

Raise an exception if the needle is missing.

If there is more than one occurrence then return the smallest index to the needle.

Extra credit
Return the largest index to a needle that has multiple occurrences in the haystack.

See also
 Search a list of records

declare
  %% Lazy list of indices of Y in Xs.
  fun {Indices Y Xs}
     for
        X in Xs
        I in 1;I+1
        yield:Yield
     do
        if Y == X then {Yield I} end
     end
  end
 
  fun {Index Y Xs}
     case {Indices Y Xs} of X|_ then X
     else raise index(elementNotFound Y) end
     end
  end
 
  Haystack = ["Zig" "Zag" "Wally" "Ronald" "Bush" "Krusty" "Charlie" "Bush" "Bozo"] 
in
  {Show {Index "Bush" Haystack}}
  {Show {List.last {Indices "Bush" Haystack}}}
 
  {Show {Index "Washington" Haystack}} %% throws