I know we can create somewhat of a similar function to a while loop using recursion and case keyword. However, how can we create a while loop with multiple conditions such as the following in Elixir:
a = [1,2,3,4,5]
b = [5,4,3,2,1]
i = 0
while (length(a) > 0 and a[length(a)-1] == b[i]){
a.pop()
i += 1
}
A noob at this so any help would be appreciated. Thank you!
In Elixir, everything is immutable, so even if there is a.pop(), it won’t work as you expected.
According to your code, I guess what you want to do is find the index of the first pair of identical elements from reversed a and b. If I’m right, then the code can be
a = [1,2,3,4,5]
b = [5,4,3,2,1]
i =
a
|> Enum.reverse()
|> Enum.zip(b)
|> Enum.find_index(fn {ea, eb} -> ea == eb end)
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