Skip to content

Instantly share code, notes, and snippets.

@thiagokokada
Last active August 15, 2018 02:22
Show Gist options
  • Save thiagokokada/4f7cbec7478a342315def01659f8f0bc to your computer and use it in GitHub Desktop.
Save thiagokokada/4f7cbec7478a342315def01659f8f0bc to your computer and use it in GitHub Desktop.
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