Showing posts with label hacking. Show all posts
Showing posts with label hacking. Show all posts

January 6, 2011

I'll be blogging again soon...

I just got back to work after a lovely, lovely break, and will get back to blogging Real Things soon. In the meantime, recording of a milestone:

I had what I like to think of my "first" "release" of ScrabbleCheat. In a match of Scrabble without it, Madly Brilliant beat me by 97 points. I used it successfully for the first time (with her knowledge and permission) and beat her the following game by about 83 points. There's still tons of work to be done, but its core is there, certainly.

I learned a ton from writing it, and hope to create a few animations/blog posts about it in the near future. This flowchart to writing Good Code, by xkcd, is relevant, mostly in relation to the quality differential between the hacktacular client code and less hackey server code.

November 7, 2010

THE LAND OF LISP

It's been a while since I've been able to run through a code book, with life throwing me so many welcome curveballs since the summer. But I'm finally taking the time to work through a book like old times; this go-round is with Conrad Barski's Land of Lisp. I probably wouldn't have gone for it, but the music video he made was just too cute:



The book is peppered with cute cartoons and a whimsy feel that is endearing, not overbearing. I highly recommend it, if only for the comics (though working through the examples is fun, the games you make really highlight Lisp's strengths).

Some may recall that I've dabbled in CL before; sadly that affair was over before it started (curveballs!), but my next project after ScrabbleCheat will likely be in either Haskell or Common Lisp ^_^

And how goes ScrabbleCheat? Quite well, actually! The technologically adventurous can download and play with it now. What I'm working on now is a simple UI in ncurses, which will probably take another week or two. But as soon as that's up, the program should not only be functional (which it mostly is now), but usable!

September 30, 2010

Some Professor Layton Prolog!

Time to combine two of my favorite things: Professor Layton and obscure languages! There was one puzzle (of the hardest difficulty) that I could have spent 40 minutes working out, but instead spent an hour and half solving in Prolog (for those unfamiliar with Prolog, check out this post for a description of what this magic language is!).

I'll post the puzzle and solution here; hopefully someone finds it fun!


No 151 from Professor Layton and the Diabolical Box: Colin's Score


Four students took a test where every question had two possible answers, A or B. Each question was worth 10 points, for a total of 100 points.

The students' test results were posted as seen below, but the teacher forgot to tally Colin's score. Colin was heading to the teacher's office when Mary called him back, saying they could figure out his score using the results from the other tests. Can you figure out Colin's score?

Mary: 70 points


12345678910
BBABABBABB


Dan: 50 points


12345678910
BAAABABAAA


Lisa: 30 points


12345678910
BAAABBBABA


Colin: ?? points


12345678910
BBAAABBAAA

-----

Below is the program I wrote to solve it. Verbose by contemporary language standards, but almost no thinking required, and the answer in an instant! Note the original version didn't have so many comments; these are to guide the curious reader. Sadly, github's gists don't know how to color Prolog syntax. Also, due to this terribly lame layout, I would suggest the 'view raw' feature at the bottom of the gist to see it in plaintext glory.



Edit: I also gist-ed my Prolog poker solver, which I wrote about in my first Prolog post. It was my very first prolog, and there are probably shorter ways of doing what I did, but hey! Give it a looksee and hope you enjoy it ^_^

July 23, 2010

GADDAG and Capitalism

I decided to revisit ScrabbleCheat (took a break for administrivia), and wrote my first Wikipedia article, on the data structure I'm refitting it with, to celebrate. It's rare that you can write something on Wikipedia that isn't there, so I had to pounce.

Hope I can keep editing it with diagrams and the like, as it's a little lame at the moment. Against my better other judgment, I'm still writing it in Erlang ^_^

---

Also, while I do believe in capitalism and it's ability to generate wealth, here are a few lame things from the last few days:


Forgive the poor sound quality, but this Louis CK bit comes to mind:

June 28, 2010

Coding socially

My gravatar for ALL these sites I decided to finally get a GitHub account, since I've downloaded countless great software from it, and think it's about time I joined/gave back. So check out my profile, feel free to be my friend (or follow my project[s], whatever it is...), and check out my first "release" of a side project, a program to help you cheat in Scrabble!

Many other Scrabble cheaters have been written, but mine is in a silly functional language! Also, I have plans to make it more than just an anagram/word generator, even though that's all this release contains. You'll need an Erlang VM to run it.

I'll also mention I have a BitBucket account, but no public repositories. My video games group used Mercurial, so now that two of us have graduated we moved our project to BitBucket. I put Rat Race and FlipTile on there as private repos. If you'd like to friend, follow, or want access to those codebases, let me know ^_^

Finally, though this is on the sidebar, I'm also on Stack Overflow.

(the pic is my Gravatar, used in all these sites; a picture taken when I was in Guatemala last year, with much longer hair and a budding beard. I think it's one of the few pictures that's not unflattering with that style. I look a bit different now.)

June 23, 2010

Type Systems, From 1000 feet high

I posted a link to one of my favorite articles ever on Facebook that is now gone. Here's the reddit link; the article was about what you should know before you debate type systems, since most people have fuzzy notions of what type systems are, what they do, and their properties. He said it best, but now that it's gone, I'll go over a few points that I remember the author addressed.

(edit: Reddit provides a Wayback Machine link, so you can read the original!).

First off, what is a type system, really? This is a bit hard to answer, but the a simple way to describe it is as a mechanism to prevent your code from executing nonsense by investigating what operations you are performing to what data. An example of this would be if you had "2 + potatoes" in your code: a type system would see that you cannot add a number and the symbol "potatoes" (nonsense!) and prevent you from doing so.

Note that we've already encountered a subtle distinction that is the source of confusion: when does this happen? There are two major forms of type systems: static types and dynamic types. A static type system will investigate your code before you run it, reporting any errors it sees, whereas a dynamic type system will tell you of errors during runtime.

When most people talk about a type system, they almost always mean static types. This does not mean that dynamically typed languages don't have type systems. Far from it. Compare this 'untyped' Ruby code:


my_array = [1,2,3]
0.upto(100){ |i| puts (my_array[i] + 1).to_s }

with the 'typed' C code:

unsigned array[3];
array[0] = 1;
array[1] = 2;
array[2] = 3;
unsigned i;
for(i = 0; i < 100; ++i) {
    printf("%d\n", array[i] + "Stack Pointer!");
}

The output of the Ruby:

2
3
4
types.rb:2:in `block in
': undefined method `+' for nil:NilClass (NoMethodError)
    from types.rb:2:in `upto'
    from types.rb:2:in `
'

And the C:

3915
3916
3917
3917
1606417842
... (continues for 100 lines) ...

We see that Ruby stops and C will plow right through! (To be fair, the C compiler will warn you of the type mismatch). Ruby's type error shows that, while dynamic, Ruby does have a type system, and shouldn't be called untyped. And while C has something people call a type system, it doesn't really function as one might expect.

Which brings up the next point: What should a type system do?. There are two major properties that a type system should strive to provide (incidentally, C/C++/Java don't provide these):

  • Progress: A well-typed expression can be evaluated further, unless the computation is finished. In short, if the expression is well typed, the rules of evaluation and type system guarantee "there's something we can do with it" (this excludes exceptions: "1/0" is well-typed, but we can't detect this beforehand without solving the Halting Problem).

  • Preservation: Evaluation of a well-typed expression leads to another well-typed expression.


Type systems that provide these properties mean that if your program passes the type checker, it can't "go wrong.", by that we mean, "the computer will always know what do to (progress), and never lead you into a false corner (preservation)." This doesn't mean your program will be bug-free, just that any bugs are logical bugs, or unhandled exceptions (basically, your own fault).

Languages with great type systems (SML, OCaml, Haskell) have proven these properties about their type systems, and it makes programming in those languages a joy.

My favorite part of the article, however, was the Fallacies section. Things people believe which just aren't true. I covered the one of the biggest ones already with the example ("dynamic typing means untyped!"), but here are two others that really get my goat:

  • Typed code is longer, more verbose. This, again, is untrue. Most people saying this are referring more to type annotations, which is text you write in your program to tell the compiler what the type of everything is. You'll find these in C, C++, Java, and C#; I once had a really ugly line of Java that looked something like:


    HashMap<String, ArrayList<Integer>> scoreMap = new HashMap<String, ArrayList<Integer>>();


    (I've ranted about Java's verbosity before). You won't find lines like that (or at least they're not mandatory) in SML, Haskell, or Scala. Using technology from 70's, we can infer types from the context of the code. So those ranting about statically typed code being verbose should really rant against type annotations, which are distinct.

  • Strong typing vs. Weak typing. THESE TERMS MEAN NOTHING. At least, nobody's agreed upon what they should mean. Even Wikipedia agrees with me. So stop saying it, and say what you mean; it's like saying a food is 'flavorful.'


It's a pity the original link is gone, he said a lot more than I did, and a lot more clearly. Still, type systems are fun, and go a lot deeper than this. If you're looking for a good introduction to programming with types, The Little MLer is hard to beat. For the theory, people seem to love Pierce. I'm not too far into it, but it looks promising.

June 8, 2010

Keep Up that Racket!

PLT Scheme, formerly my favorite Scheme implementation and mentioned before in my writings, has been re-branded as Racket. I'm very excited about this: Racket is a language of unbelievable potential, and hopefully it's re-branding will make people aware of this.

(while it is just a name change and new website, I doubt Clojure would have gotten it's momentum if it were just called "JVM Lisp").

One of the wonderful things about Racket is its mailing list, and a cute discussion there generated a major treasure. Namely, someone brought up that computer programming isn't really that related to computer science, to which someone else mentioned that you could very much be a successful programmer even if you haven't studied the science.

This is sadly true (many people making their livings don't know what they're doing, many examples at The Daily WTF), but then a user named Joe Marshall simply wins:


> It's quite possible to be a productive and successful programmer without having a
> solid understanding of computer science.

That's the problem. Maybe it shouldn't be the case. Variations on this
statement are alarming:

"It's quite possible to be a productive and successful physician without having a solid understanding of medicine."

"It's quite possible to be a productive and successful airplane engineer without having a solid understanding of aerodynamics."

'Rocket Scientist' : 'Newtonian physics'
'Brain Surgeon' : 'neurology'

I attribute this more to the fact that we're living in the cave-writing stages of software: we've had computers for fewer than 70 years, personal computers for maybe 30, and connectivity for less. I'm sure after the invention of the steam engine, all sorts of idiots were designing inefficient, dangerous factories, and we've now got the software equivalent.

I'm lucky in that I got to study the science, and am genuinely interested in it to keep learning. But what a wonderful day when people who develop software who know at least what I do will come standard.

June 1, 2010

Really, Apple?

Apple just showed themselves to be a bit lame about security, at least on Mac OS X two years ago. Here's the story:

My sister had a MacBook to use for college when she was supposed to enroll in 2008. A few weeks later she got sick with her encephalitis, and had her remarkable recovery story.

Unfortunately, she didn't remember the root password she used for her laptop, and it wasn't any of her 'standards' (we'd tried them all). So while she could still use the computer via the auto-login function, she couldn't install or update software (since this requires an authorized user to enter their password). Regarding use of passwords, this was a stranger's computer.

This presented mild problems over the last few years, but this came to a fore last night: with a very outdated iTunes, she couldn't sync her iPhone on her laptop while a summer student at Columbia's School for Continuing Education. This is on top of using and outdated and insecure OS, being unable to install new software...

So I googled "recover lost root password Mac OS X," and lo and behold, the instructions I found worked!

This should not happen. I shouldn't be able to set or reset root passwords with physical access to a stranger's computer. Below is the fix, in both technical terms and non-technical metaphor terms, for the curious:

Technical: Holding Command-S on startup allows you to run in single-user mode, as root. So a simple mount -uw / followed by passwd root allowed me to set the password of user 'root' to letmein. Then I reboot, and log into Mac OS X normally with admin privileges as user root.

Non-technical Metaphor: Suppose you're building is guarded by a lazy doorman. He has a list with all the tenants in the building, but when you walk up to him, you notice he just looks at the placards on the mailboxes. I effectively scribbled 'root' on a postcard, taped it to a mailbox, and walked up to the guard the next day and said "My name is root!" Seeing it written on a mailbox, he let me in and I changed the locks on a stranger's apartment.

While I haven't felt that elated in years (and saved my sister's computer from having its reset to disk factory settings, our best alternative), this is a major lame sauce from Apple.