Created
December 5, 2010 13:47
-
-
Save prophile/729093 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
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