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

to indexof :item :list
  if empty? :list [(throw "NOTFOUND 0)]
  if equal? :item first :list [output 1]
  output 1 + indexof :item butfirst :list
end
 
to showindex :item :list
  make "i catch "NOTFOUND [indexof :item :list]
  ifelse :i = 0 [(print :item [ not found in ] :list)] [(print :item [ found at position ] :i [ in ] :list)]
end
 
showindex "dog [My dog has fleas]   ; dog found at position 2 in My dog has fleas
showindex "cat [My dog has fleas]   ; cat not found in My dog has fleas