I have the string \"1001\" and I want the string \"9\".
The numeric library has the (rather clunky) showIntAtBase, but I haven\'t been able to find the opposite.
Because
1001 = 1 * 2^0 + 0 * 2^1 + 0 * 2^2 + 1 * 2^3 = 1 + 0 + 0 + 8 = 9
┌───┬───┬───┬───┐
│1 │0 │0 │1 │
├───┼───┼───┼───┤
│2^3│2^2│2^1│2^0│
└───┴───┴───┴───┘
so obviously:
fromBinary :: String -> Int
fromBinary str = sum $ zipWith toDec (reverse str) [0 .. length str]
where toDec a b = digitToInt a * (2 ^ b)