Skip to content

Instantly share code, notes, and snippets.

@jszmajda
Created April 7, 2016 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jszmajda/7f038c2ede60ed2433ffef14ce3579f2 to your computer and use it in GitHub Desktop.
Save jszmajda/7f038c2ede60ed2433ffef14ce3579f2 to your computer and use it in GitHub Desktop.
Binary Conversion in Elixir
# Binary conversion: take a number 7, output "111"
defmodule Randori do
def helper(x, s \\ "")
def helper(0, "") do
"0"
end
def helper(0, s) do
s
end
def helper(x, s) when rem(x,2) ==0 do
helper(div(x, 2), "0" <> s)
end
def helper(x, s) do
helper(div(x, 2), "1" <> s)
end
def conversion(x) do
helper(x)
end
end
IO.puts "started"
IO.puts Randori.conversion(7)
IO.puts Randori.conversion(2)
IO.puts Randori.conversion(0)
IO.gets "what is the number? "
IO.puts "ended"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment