Created
April 7, 2016 14:39
-
-
Save jszmajda/7f038c2ede60ed2433ffef14ce3579f2 to your computer and use it in GitHub Desktop.
Binary Conversion in Elixir
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
# 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