Saturday, December 26, 2009

So, generic programming

This is a good read.

It's an interview with Alexander Stepanov, and contains gems like:
"I find OOP technically unsound. It attempts to decompose the world in terms of interfaces that vary on a single type. To deal with the real problems you need multisorted algebras - families of interfaces that span multiple types. I find OOP philosophically unsound. It claims that everything is an object. Even if it is true it is not very interesting - saying that everything is an object is saying nothing at all. I find OOP methodologically wrong. It starts with classes. It is as if mathematicians would start with axioms. You do not start with axioms - you start with proofs. Only when you have found a bunch of related proofs, can you come up with axioms. You end with axioms. The same thing is true in programming: you have to start with interesting algorithms. Only when you understand them well, can you come up with an interface that will let them work."
"You can't write a generic max() in Java that takes two arguments of some type and has a return value of that same type. Inheritance and interfaces don't help. And if they cannot implement max or swap or linear search, what chances do they have to implement really complex stuff? These are my litmus tests: if a language allows me to implement max and swap and linear search generically - then it has some potential."
Good stuff; making me think.

Thursday, December 24, 2009

Customizing the blog a bit

This layout has always been rather insistent that I have a very thin middle column in which to place my posts. It's bugged me for a long time, but I decided that messing with the CSS template was outside of my competence level and just asking for trouble.

At the same time, I've always wanted to be able to place code into the blog without it looking like some awful monster crapped out unformatted text.

Well this morning I've fixed these issues.

The latter was as easy as googling "showing code in blogspot" and then following the instructions at the top link.

Essentially, you go to the Layout section of your dashboard, click "edit HTML", find a tag "]]></b:skin>", and insert some CSS in the section above it. It adds the necessary stuff to make pre and code tags to work wonderfully.

Then it recommends running code through a converter to quickly change it to escaped-text before posting it.

I feel like (and am, really) a script kiddie but hey, I've got code showing up in my blog like so:

int main(int argc, char *argv[])
int a, b, c;
int *d = NULL;

a = 5;
b = 6;
c = 7;
d = malloc(sizeof(int));
d[1] = 8;

if(argc == 2)
printf("What a lame example!\n");

return 0;
which is wonderful.

Then I got to thinking "Hey, I'm a cocky CSS editing fiend, why not solve the problem of the blog's lacking width?" And I looked over the code in the layout section.

I found in there (this is likely specific to this template)
#header-wrapper {
margin:0 auto 10px;
border:1px solid $bordercolor;
#main-wrapper {
width: 410px;
float: $startSide;
word-wrap: break-word; /* fix for long text breaking sidebar float in IE */
overflow: hidden; /* fix for long non-text content breaking IE sidebar float */
#footer {
margin:0 auto;
line-height: 1.6em;
text-align: center;
These all have 'width' attributes that look like they're big enough to be most of the screen, and they're the only similar ones in the file. 660 pixels isn't very wide - I imagine it was set at that to work on an 800x600 screen, should one need to see my page.

Well you know what? I'm officially leaving those people in the dust.

Following some common design advice of 960 pixelwidth pages (see, I decided to directly increase 660 to 960, and 410 (correspondingly) to 710. This widened the main post section of my blog considerably, and seems to have had no negative consequences.

Hooray for a productive blogging morning.

Python Fun

Every programmer, when learning something new, hits on that devilish quandary:

"What should I program?"

And I am a man like all others, who twiddles my thumbs and wonders "ah... if only I had something to /do/ with this language, I would really start to learn it!"

Well, I've decided to just dink around uselessly and see what comes of it. Then share.

The things we'll cover:
  • Some stupid string stuff
  • some more stupid string stuff, with numbers involved
  • then some regular old math

stupid string stuff:

First I started off playing with the string and list functions, and interchanging between them. Throughout this, I might duplicate existing functionality -- if I do, comment and tell me how to do it better! I'd love to learn.

I decied that I wanted to take a string, say "Hey man, what's up?" and insert stuff between each letter. I couldn't really find a quick way to do this, but some related things popped up.
a = "Hey man, what's up?"
will give you ['Hey', 'man', 'what\'s', 'up?'] - and that's nice, but I want things broken down to the letter. So I wrote a function breakout:
def breakout(input):
r = []
for i in input:

return r
breakout("hello") returns ['h','e','l','l','o']

then you can do a str.join() - let's wrap it into a function!
def splitByChar(instring, splitchar):
return splitchar.join(breakout(instring)
you can, through this, call splitByChar("hello", '.') and receive 'h.e.l.l.o'

ah, stupid pointless string stuff.

more stupid string stuff, with some math

Next up, I decided to play with phone numbers. I decided to break them up into their constituent digits and add them. This was (as many things in python are), an easy task.
def summit(input):
sum = 0
for i in input:
if i.isdigit():
sum += int(i)

return sum
This function runs quickly through the string and adds to the sum for every integer found.

I put in my phone number, print(summit("519-703-3336")), and received 42. Exciting!

Then I got to wondering.... I wonder how many phone numbers out there sum to 42? I wonder what the distribution is like across the rang of sums from 0 to 90? Let's write some code to find out:
def smattering(input):
arr = []
for i in range(0,9*input):

temp = []
for i in range(0,input):

upper = int(''.join(temp))
upperStr = str(upper)
for i in range(0, upper):
arr[summit(str(i))] += 1
if i % 1000000 == 0:
print(str(i) + ", " + str(upper - i) + ", " + str(i/upper))

return arr
This code makes a 90-element list, then brute-force walks from 0 to 9,999,999,999 and calculates every single sum, stopping every million steps to print out how far we are in, how far we have left, and a percentage through the path we've walked. I did some simple calculations, every printout occurs about 9 seconds apart, and 9,999 of them are required to take the calculation to completion.

This translates to about 24.9 hours. That's a pretty long calculation -- and it's kind of required; summing each of 10 digits for each of 10 billion numbers is 100 billion operations no matter what way you cut it. I've left the calculation running in a terminal (two, actually - one which will conclude and print the values to the screen and to a text file, and another in an interactive terminal in case I come up with fun things I want to do to the list off the bat) - but I'm beginning to think

some regular old math

is in order to find out the answer to this question a bit faster. The clock is ticking, and I have a day to find out. I'll update once I've thought about it a bit.

Wednesday, December 23, 2009

Hard to settle

So far I've played with Python, learned some piano, written a program to help me learn large-number multiplication, played hours of Ogre Battle 64, and watched the first season and a half of Weeds. Also seen friends, walked many kilometers, and slept gratuitously.

I think a haircut is in order. Maybe not today, maybe the 27th or so. Also, laundry.

I'm going to move forward with watching the SICL videos, and then dive into the Graphics/Language stuff. Math can happen in my spare time, as can piano. I've also got a phone-interview for an internship today. I'm not certain quite what to expect - algorithms and code-related questions, a history of myself, or just good discussion. I hope it goes well.

It's been wonderful to have time not committed - when I decide I want to do something, I can generally just go do it. I've learned a lot and I feel much happier than I have in weeks.

I also fixed up my home-server's webpage, have read a bunch of my various books (Contact, The God Particle, the latest Wheel of Time book), listened to a TON of the music I've accrued over the past semester, and played with some VMs. Installing Slackware is an interesting experience to say the least.

I'll post back in a few days (likely just post-christmas) about my progress on SICL, Python, Graphics, and my C-spitting-extensions to C.

Wednesday, December 16, 2009

A flurry of activity

Well, that's another semester past.

I have from right now until January 10th to myself, effectively. It's time to decide how to use it.

So from what I can tell, my options are:

Base Skills
Something Complex

Graphics would be making computers do graphical things for me and also making graphics myself. Playing with Blender, with the GIMP or Photoshop, with Paint and maybe Flash, reading about colour-theory and how to paint a perdy picture. I'd also be drawing a lot, and reading about basic art stuff. And of course, playing with C/OpenGL to make the computer do cool things, and learning related math (Matrix Algebra, anyone?)

Languages would be reading about and learning Lisp and Python through the development of simple (and slightly more complicated) projects with them. I would watch the SICL videos from MIT's Open Courseware, generally get a better feel for Lisp, and I'd just do stuff with Python. I would like to implement my scope/stack/reference/object stuff for C.

Basic Skills would be math and physics and chemistry and maybe biology, just learning voraciously over the break -- very largely just improving my mathematical skill and comfort level. I would also be spending a lot of time working on music and some basic technology things, like awk/sed and being a better vimmer, using the full extent of regexp's and scripting.

Something Complex would be choosing a project - I'd spend a few days choosing, then go through a design phase and build myself something good. Something that I could sell, or at least use in my day to day life. Maybe this could include working on an Open Source project, or attacking Sam Altman's company Loopt, which just does not seem to be doing a good job with my idea.

Anyway, one of those four things. I'll have to ask myself "Where do I want to be on January 10th", and my answer could be "I want to walk up to a Piano and lay down the driving rhythm line from 'Don't Stop Me Now'.", or "I want to be able to take 4 files of similar data in disparate formats and spit them sorted into a single new file, every 10 seconds or when the file exceeds 100 lines, and I want to be able to write the thing to do it in about two minutes.", or maybe "I want to be able to spend a weekend and make a simple game, with my own pixel graphics, my own physics, my own everything, and run it on Linux or Windows.", or maybe "I want to be able to do what I want to be able to do. In Lisp. (really poor recursion joke)". Ooh, or, "I want to be able to read a Wikipedia article on a mathematical topic and not have it go over my head before I'm halfway down the page."

So, which of those sounds best? Or at least, sounds like the one I'd like first? I'm going to spend an hour or two thinking about it.