# Programming Elixir 1.3: Exercise: Functions-1

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
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!

Hi,
Basically speaking you need to know two things:

• how to traverse list using pattern matching/recursion -> maybe write some test function that will print all elements of the list
• second you will need to know pattern in FP: recursion + accumulator, accumulator is used to store state between function calls, and of course you will need to know how to write recursion functions
https://elixir-lang.org/getting-started/recursion.html
2 Likes