I am following the official Elixir guide. I am currently on the chapter “9. Recursion” and things are going relatively well.
However, I was trying to run the double_each
example (it’s almost at the end) not in iex but in the terminal via elixir double_each.exs
.
The code is:
defmodule Math do
def double_each([head | tail]) do
[head * 2 | double_each(tail)]
end
def double_each([]) do
[]
end
end
Math.double_each([3,4,5])
|> to_string
|> IO.puts
But the output is just an empty line.
When I tried to modify the code to this:
defmodule Math do
def double_each([head | tail]) do
[head * 2 | double_each(tail)]
end
def double_each([]) do
[]
end
end
Math.double_each([3,4,5])
|> to_string
|> IO.inspect
I got <<6, 8, 10>>
as the output in the terminal.
Is it possible to somehow convert the output so it can be printed out via IO.puts?
Thank you in advance.