mirror of
https://github.com/wnagrodzki/ProgrammingElixir1.6-MyTurns.git
synced 2025-05-03 17:41:41 +02:00
17 lines
No EOL
391 B
Elixir
17 lines
No EOL
391 B
Elixir
# Implement and run a function sum(n) that uses recursion to calculate the sum of the integers from 1 to n.
|
||
# You’ll need to write this function inside a module in a separate file.
|
||
# Then load up IEx, compile that file, and try your function.
|
||
|
||
defmodule MyModule do
|
||
|
||
def sum(1) do
|
||
1
|
||
end
|
||
|
||
def sum(n) do
|
||
n + sum(n - 1)
|
||
end
|
||
|
||
end
|
||
|
||
IO.puts MyModule.sum(10) |