Echoing everything @Aetherus said, with a similar example but just using recursion:
defmodule Match do
@a [1,2,3,4,5]
@b [5,4,3,2,1]
# Elixir has lists, not arrays, and therefore
# its O(n) to access elements and typically this
# is not what you want. For this example, reversing
# the second list once is far more efficient
def reverse(a \\ @a, b \\ @b) do
a
|> :lists.reverse()
|> reverse(b, 0)
end
# length(a) == 0
def reverse([], _, count) do
count
end
# the head of each list is the same fulfilling
# a[length(a)-1] == b[I]
# notice we are pattern matching the head of each list
# and proceeding only if they are the same
def reverse([first | a_rest], [first | b_rest], count) do
reverse(a_rest, b_rest, count + 1)
end
# When the head of the lists no longer match we return
def reverse(_, _, count) do
count
end
end






















