fork download
  1. defmodule LinearSearch do
  2.  
  3. def search(list, target) do
  4. search_with_index(list, target, 0)
  5. end
  6.  
  7. defp search_with_index([], _target, _index), do: nil
  8.  
  9. defp search_with_index([head | tail], target, index) do
  10. if head == target do
  11. index
  12. else
  13. search_with_index(tail, target, index + 1)
  14. end
  15. end
  16. end
  17.  
  18. # Пример использования
  19. arr = [12, 1456, 53, 12451, 992, 81, 666]
  20. target = 992
  21.  
  22. case LinearSearch.search(arr, target) do
  23. nil ->
  24. IO.puts("The #{target} element was not found")
  25.  
  26. index ->
  27. IO.puts("The #{target} element was found at position #{index}")
  28. end
Success #stdin #stdout 0.38s 40956KB
stdin
Standard input is empty
stdout
The 992 element was found at position 4