Last active
August 15, 2018 02:22
-
-
Save thiagokokada/4f7cbec7478a342315def01659f8f0bc to your computer and use it in GitHub Desktop.
This file contains 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, io \\ IO) do | |
interpret(acc, io) |> repl(io) | |
end | |
def interpret(acc, io) do | |
io.puts("accumulator> #{acc}") | |
[command, value] = io.gets("operation> ") |> 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