Skip to content

Instantly share code, notes, and snippets.

@prophile
Created December 5, 2010 13:47
Show Gist options
  • Save prophile/729093 to your computer and use it in GitHub Desktop.
Save prophile/729093 to your computer and use it in GitHub Desktop.
module xtoa;
export xtoa;
export hexValue;
export decValue;
export binValue;
xtoa :: String, Int -> String;
hexValue :: Int -> String;
decValue :: Int -> String;
binValue :: Int -> String;
binValue(val) := xtoa("01", val);
decValue(val) := xtoa("0123456789", val);
hexValue(val) := xtoa("0123456789abcdef", val);
xtoa(alphabet, val) {
base = length(alphabet);
negative = false;
case compare(val, 0) {
@less {
negative = true;
val = -val;
}
@equal {
return alphabet !! 0;
}
@greater {}
}
output = "";
while val > 0 {
output = [alphabet !! (val % base)] : output;
val = val / base;
}
if negative {
output = "-" : output;
}
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment