Created
August 15, 2018 02:36
-
-
Save thiagokokada/3170cd495f625dd3e330da1f1b5415ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Calculator do | |
def repl(acc \\ 0.0, i \\ 0, io \\ IO) do | |
interpret(acc, i + 1, io) |> repl(i + 1, io) | |
end | |
def interpret(acc, i, io) do | |
io.puts("accumulator> #{acc}") | |
[command, value] = io.gets("operation(#{i})> ") |> String.trim() |> String.split() | |
{value, _} = Float.parse(value) # Convert a string to its float representation | |
run_command(acc, command, value) | |
end | |
defp run_command(acc, command, value) do | |
case command do | |
"=" -> value | |
"+" -> acc + value | |
"-" -> acc - value | |
"*" -> acc * value | |
"/" -> acc / value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment