tehgeekmeister’s blog

January 13, 2008

huh. google knows about me now, go figure.

Filed under: Uncategorized — Tags: , , — tehgeekmeister @ 10:08 pm

apparently submitting that last post to reddit has gotten my previous post to be in the first page of google results for “binary reader“. cool, i’m fine with the inevitable traffic from that.

have finished serge lang’s introduction to linear algebra. a good book on the subject, but i can’t say the subject itself interests me horribly. about to read a first course in calculus by the same author.  calculus should be more interesting.

noticed this post on reddit.  considering, in a few months, or whenever i’m finally up to the challenge, going over the source and attempting to get involved in the development.  contributing to an actual project would be useful and quite the learning experience, but i don’t really have any preference as to what i do or what sort of a project i work on, just that it be something i can actually manage learning on my own.  xmonad may be better for this, potentially — but i don’t use linux, so i’d have to set up a vm just for the purpose.  seems like overkill.  we’ll see.  if anyone has any suggestions of things that need doing or projects that are easier/more useful to start off with, let me know.

aside from that, expect to see a preliminary version of my flowchart app in a post soon, and hopefully in active development after that.  i’ve added in manual state threading, and at this point only need to start writing some code that uses it in order to figure out where the hell i need to go next with it.  also, there’s another grand scheme this fits into, but, well…  that’s another post for when that happens, if ever.

January 11, 2008

one line binary reader in haskell

Filed under: computer science, math, programming — Tags: , , — tehgeekmeister @ 4:22 pm

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.

Blog at WordPress.com.