update: apparently this is now in the first page of google results for “binary reader”, so i feel obligated to point out that i posted an update on this, with better versions thanks to commenters both here and on reddit.

i post this in the tradition of pointlessly showing how concisely one’s favorite language can perform a given task. also, i’m slightly proud of this version, as my previous wasn’t nearly as to the point (and wasn’t point free!). so, here it is:

readBin = (`div` 2) . foldl (\x y -> (x + y) * 2) 0 . map (\c -> case c of {‘0’ -> 0; ‘1’ -> 1; _ -> error “Input is not a binary string.”})

previous to this, i was using:

readBin xs = core (map bin xs) 0

where

core [x] n = n + x

core (x:xs) n = core xs ((x + n) * 2)

bin c = case c of {‘0’ -> 0; ‘1’ -> 1; _ -> error ” Input is not a binary string.”}

which, while absolutely equivalent, is overly verbose. now i’ll explain the algorithm for those of you to whom it’s not blindingly obvious (i’m not doing anything tricky or special here, i swear. i’m not that cool.). so, first off, we’re mapping over this string (strings are lists of chars in haskell), and converting each ‘0’ or ‘1’ to the 0 or 1, respectively. also, if we come across something aside from a ‘0’ or ‘1’, we raise an error. once the string is converted to a list of ints, we consume the first value of the list, add it to an accumulator, and multiply that by two. why? because by definition any binary integer has the decimal value of the sum of all the digits, going from right to left, to the nth power, where n is the index starting from zero of the right to left position in the binary representation.

so, wait, if we’re summing and raising to the nth power each digit from right to left, then why are we using a left fold (foldl)? because, in this case, adding the digit to the accumulator (x in the lambda passed to foldl) and then multiplying that times two is exactly equivalent to the previous definition i gave of the decimal value of a binary integer. i’ll leave figuring out exactly why that’s the case to the reader. also, in case you didn’t notice, the fold i’m using in the first definition does one more multiplication by two than the other version, which is why, to reap the benefit of a more elegant, pointfree version, we must divide by two at the end.

the point, on the whole, of this? well, as i already said, it’s pointless. but if there were to be one, it’d be this: damn, haskell is cool. i couldn’t have ever done that in other languages i’ve used. (but i’m sure someone else could. i’m just not that cool.)

p.s.: if anyone finds any flaws with any of what i’ve said here, please take the time to comment — while this algorithm has passed a few test runs, and it seems right intuitively, i’m just not experienced enough to be sure it actually is.