I have been given this code by our teacher. I am having trouble following what this code does. This is all I know so far: [x1, y1 | z1] = Output2.abc(3)
is called, and so function abc(2)
will spawn a new process assigned as y
. Then it will send the value 2 to y
. When it receives 2, I am stuck at what receive is doing. What does z -> z
mean?
Also, the prof asks what `x1, y1 are. I don't understand where these variables are located in this code. If someone can just guide me through this it would be much appreciated. Thanks
defmodule Output2 do
def abc(x) do
y = spawn_link(__MODULE__, :n, [self()])
send y, x
receive do
z -> z
end
end
def n(z) do
receive do
v -> send z, n(v * v, v)
end
end
defp n(x, x), do: [x]
defp n(x, y), do: [y | n(x, y + y)]
end
[x1, y1 | z1] = Output2.abc(2)
Output2.abc(2)
is called.n(z)
as the receiver
n(v * v, v)
n(v * v, v)
is a call to n(x, y)
because x, and y are different values. n(2*2, 2)
. n(x,y)
returns a list of y
concatenated with n(x, y+y)
where x = 4, and y = 2n(4, 2+2)
is called, which invokes n(x, x)
returning a single item list of [4]z
, and returns z
(z -> z
)