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 12, 2008

better one line binary readers in haskell, thanks to commenters and redditors.

Filed under: programming — Tags: , — tehgeekmeister @ 6:38 pm

update to my previous post on binary readers.

from roconnor:

readBin s = x

[(x,"")] = Numeric.readInt 2 (`elem` "01") Char.digitToInt s -- probably the best way of doing it; reinventing what's available in the standard libraries isn't a good thing.

and two from pjdelport:

readBin = foldM (\a d -> (a*2 +) <$> elemIndex d "01") 0 -- as elemIndex evaluates to a Maybe, (<$>) or fmap is necessary to lift the arithmetic section into the Maybe. bonus points for using Maybe instead of error. readBin = (foldl' ((+) . (*2)) 0 <$>) . mapM (`elemIndex` "01") — this version separates “parsing” and summing the values of each bit. otherwise same benefits as the previous versions.

that’s it for now.

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
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.

January 5, 2008

obligatory monthly post

Filed under: math, Uncategorized — tehgeekmeister @ 10:18 pm

it seems i’ve been posting just about monthly so far. this is a decent pace — i expect it’ll get better.

nearly done with my preliminary studies of linear algebra: have ditched the previous course in favor of serge lang’s (hmm, have i mentioned this already? *checks. yes, i have.) okay, more up to date then, i’m actually reading lang’s introduction to linear algebra. while beezer’s text was good, it seems to be aimed more towards those studying math as a requisite for other studies rather than those interested in math itself (i don’t say math major or non-major, because, well, i’m not in college). this just wasn’t what i wanted. at the same time, lang’s book is much to my liking in being concise (at times a touch too concise, but nothing unconquerable if you’re dedicated and ready for linear algebra itself).

i’ve been finding myself bored with the internet lately; this is probably a good sign, as it means i’m using the internet as a tool for studies and communications and not as an end in itself. oddly, this might mean more posts. we’ll see. not like there’s anyone really dying for the next post.

i’m off to begin reading the hobbit and go to bed now.  potentially more later.

Blog at WordPress.com.