Skip to content

Instantly share code, notes, and snippets.

@manonthemat
Created May 21, 2014 18:28
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 manonthemat/37231afb95252bfba76b to your computer and use it in GitHub Desktop.
Save manonthemat/37231afb95252bfba76b to your computer and use it in GitHub Desktop.
simple math fun with haskell - custom number base systems
import Data.Char (digitToInt)
numberToBaseString :: Int -> Int -> String
numberToBaseString n base
| n < base = show n
| otherwise = numberToBaseString ((n - (n `mod` base)) `div` base) base ++ show (n `mod` base)
baseStringToValue :: String -> Int -> Int
baseStringToValue n base
| n == [] = 0
| otherwise = base * (baseStringToValue (init n) base) + digitToInt (last n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment