module Binary
( toDecimal
) where

import Data.Map (findWithDefault, fromList)

toDecimal :: String -> Int
toDecimal = convert "01"

convert :: String -> String -> Int
convert digits string = sum parts where
    radix = length digits
    digitValues = fromList $ zip digits [0..]
    values = map getValue string
    getValue digit = findWithDefault 0 digit digitValues
    pairs = zip (reverse values) [0..]
    parts = map exponentiate pairs
    exponentiate (value, position) = value * radix ^ position

module Binary
( toDecimal
) where

import Data.List (foldl')

toDecimal :: String -> Int
toDecimal = convert fromBinary 2 where
    fromBinary = fromEnum . ('1' ==)

convert :: (Char -> Int) -> Int -> String -> Int
convert fromBase radix string = foldl' accumulate 0 values where
    values = map fromBase string
    accumulate number digit = number * radix + digit

Change solution to use foldl'
Change solution to use foldl'
