Hello,
I’m currently attempting to do the exercises from Programming Elixir 1.3 and I’m looking for some feedback and some help in order to do the sum function. I didn’t do any functional programming before.
Here’s what I’ve tried:
defmodule Solution do
def hello_world do
IO.puts "Hello World!"
end
# 1. List Concat. list_concat.([:a, :b], [:c, :d]) #=> [:a, :b, :c, :d]
def list_concat([ head | tail ], acc) do
list_concat(tail, Enum.concat(head, acc))
end
def list_concat([], acc), do: acc
end
Solution.hello_world
IO.inspect Solution.list_concat [ [1,2,3], [4,5,6] ], []
# 2. sum.(1, 2, 3) #=> 6
# sum = fn a -> Enum.map a, &(&1 + 2)
# IO.inspect sum.(1)
# 3. pair_tuple_to_list.( { 1234, 5678 } ) #=> [ 1234, 5678 ]
pair_tuple_to_list = fn { a, b} -> [a, b] end
IO.inspect pair_tuple_to_list.( { 1234, 5678 } ) #=> [ 1234, 5678 ]
I don’t know how to do the anonymous sum function, how do I iterate over all the parameters and return the sum of them?
PS: Looks like I’m overcomplicating things: https://forums.pragprog.com/forums/322/topics/Exercise:%20Functions-1
Thank you!