ProgrammingElixir1.6-MyTurns/ModulesAndFunctions-4.exs

17 lines
No EOL
391 B
Elixir
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Implement and run a function sum(n) that uses recursion to calculate the sum of the integers from 1 to n.
# Youll 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)