Category Archives: Programming

My first Hackathon

Last week, my friend an I went down to AOL headquarters in Palo Alto to compete (more like participate) in AngelHack, a hackathon with four simultaneous events around the country whose winners would go on to subsequently compete for angel funding from VCs and entrepreneurs in the tech industry.

Although I organized a hackathon at Yale last semester, I found that participating is (unsurprisingly) a whole different beast.

First, something I learned:

There is a difference between doing and understanding. Given the time constraints and the sheer fact that your project will be evaluated on the basis of a two minute demo, your code does not need to be ‘clean’ or well-tested, and there are plenty of features for which you can simply use a plug-and-play solution rather than building it custom. Of course, if you want to turn your project into a real application, you have just left yourself with a mountain of technical debt, but for the most part, a Hackathon project is just something to make and let alone.

Something that impressed me:
The creativity involved in making a new tool, something that is not just a modification of an existing solution, but is an entirely new combination of technologies.

All told, it was a great experience that I would definitely like to repeat. There is nothing else quite like being surrounded by 200 other people who are just hacking away and having a ton of fun, even as you hit the wee hours of morning when you’re running on caffeinated soda and energy drinks.  The corollary to that, of course, is the fantastic networking opportunity — for me this realized itself in meeting Alexey Komissarouk, the UPenn graduate whose blog helped me through both hosting a hackathon and deciding what I would do this summer (this is a great article).  He happened to be sitting next to me during the hackathon, and when I overheard him introducing himself to someone else, I couldn’t help but turn and say, “hey, do you happen to keep a blog?”  You can probably fill in the rest of the story.

So beyond my project and the actual experience of hacking, that’s what I took away from AngelHack.  When it’s a little bit more polished and functional, I’ll post a link so you can check it out. But for those who are curious, it is a web app that allows a single admin create a phone directory with a list of contacts, etc and provide access to that directory via sms. A typical use case might be a parent who compiles a list of doctors phone numbers so their children can simply text “call dr. jones” to the directory number and be connected without having to lookup the number. It’s a problem I’ve run into myself, so I hope my solution can be useful to other people.

Leave a comment

Posted by on July 1, 2012 in General, Programming


Online Education with Coursera

I think it’s pretty safe to say that my readership doesn’t come anywhere near that of Jeff Atwood or Joel Spolsky, but I do awant to apologize nonetheless for the utter lack of content over the past few months. That said, I am going to comit myself to writing at least one post a week for the duration of the summer (for something a little more intense, see Alexey Komissarouk’s blog post, so we’ll see what interesting things come out of it. Should be fun.

So this first one is going to be a review of my experience with taking an online course with For those of you who don’t know, Coursera is a quickly expanding online education platform that serves up online courses from places like Stanford, UC Berkeley, Princeton, the University of Michigan, and the University of Pennsylvania (ya, I though you’ve heard of them).  All signs point to them adding more resources from more schools.

Given that I have some spare time this summer when I am not at work, and given that I can only take so many courses during a school year (plus the fact that Yale doesn’t offer CS courses in certain areas), I decided to give it a shot. I’ve tried online learning in the past, and I usually have lost interest/have not stuck with the course all the way through, mostly because there is no one mandating that I do the work, and it is all too easy for my mind to wander while watching a video.

This time was different.  I took the Software as a Service course offered by UC Berkeley professors David Patterson and Armando Fox.  Essentially, the course teaches web development with Ruby on Rails, teaching key concepts like Agile development, behavior/test-driven design, and service-oriented architecture.  In one month, I learned more about web development than I was able to do all year, and I had a number of lingering conceptual questions fall into place.  David and Armando are great professors and deserve kudos for putting together a great course (this is the second iteration of SaaS), and a lot of my learning resulted from the thoughtful way in which they prepared the course materials, but more importantly, the platform Coursera created led to a much more engaging and ultimately educational experience.

The first thing that sets Coursera apart from other online education resources like MITOpenCourseware, KhanAcademy (as a side note, you should watch Salman Khan’s commencement speech at Rice), and others, is that these are real courses.  yes, MIT provides all of the course materials (including exams and answer keys) to go along with videos, but Coursera provides all of that, AND they have the participation of the professors who created the courses, answering questions by email and on forums. And oh yea, did I mention it’s free?

I think the reason Coursera is and will continue to be successful is that they took a traditionally static online education model and made it dynamic.  Through forums that are actually tended to by volunteer TAs, questions that are actually answered by professors, and homework assignments that are actually graded by an autograder (for a programming assignment or quiz) or a real person, they added a level of human interaction and reality that has thus far been lacking in free online educational platforms of this scale.  That the video lectures are posted week by week, rather than all at once, that my homework assignments have [real] deadlines — each of these contributes to an experience that strikes a balance between being serious and enjoyable.

So that’s my take. What are your thoughts?

Leave a comment

Posted by on June 21, 2012 in General, Programming


Tags: , , , , , , , , ,

Functional Programming and Haskell

Hey everyone — sorry it’s been a while since my last post.  School got off to a crazy start, and I’ve been really busy planning a hackathon, writing material for my iOS course, and just generally being involved with life on campus.  Oh right, and I am taking a few CS courses, too.

This post in particular is dedicated to a hidden gem that I found, and one that has given me a new way to look at programming and problem solving:

In addition to taking my two core CS courses, Data Structures and Algorithms, I enrolled in a a linguistics course that explores how we can represent and manipulate meaning in a computer.  In order to do that, we are learning Haskell. It is an incredible language. Here’s where I’ve been learning, and here’s the scoop:

It’s functional. What does that mean? I had learned Scheme for my introduction to CS course, but I did not understand the essence of functional programing. It’s based on lambda calculus, which is a system for manipulating functions.  Essentially, the lambda “term” are defined as follows (you can read more here if you’re interested):

  1. Any variable or atomic statement, x
  2. Abstraction, λx . M – i.e. turn M into a function, with x as the variable
  3. Application, (M N) – i.e. replace every appearance of x in M (see 2) with N

It may seem a bit confusing, but it is quite similar to how we think about math.  You have functions, denoted f(x) or the like, and you can apply that function to x.  Alternatively, given some expression (without an equal sign), you can turn it into a function by setting it equal to g(x) or the like.  The single most powerful feature of lambda calculus is the ability to pass a function as an argument to another function.  Just like we can apply a function to a variable or a constant (1), we can also do something like f(g(x)) and get a totally new function which we can then apply to some other expression.  What’s more, and here is a place lambda calculus gets cool, is that if a function takes multiple variables, if we give it a value for one of the variables, it becomes a totally new, and perfectly legal function.  This has tremendous implications for how we can manipulate data, and gives you the opportunity to write highly flexible code.

But enough of my raving.  If you don’t believe me, read thisthis, or this.  And get started learning Haskell!  Here are my favorite things about the language:

  • Strong typing – the compiler will yell at you if your types don’t match exactly.  Seriously.  And ghc (glasgow haskell compiler) will give you very precise, useful error messages.  This prevents a lot of runtime errors, and usually means that you will have a working program once it has successfully compiled.
  • First class functions – since Haskell is functional, there is no concept of “parameters” per se, only terms to which you are applying a function.  Because of this, functions are applied to terms immediately following them, and function application takes the highest precedence.  Say goodbye to parentheses!
  • First class math – so many built in functions for math.  One line fibonacci sequence. ‘Nuff said.  Maybe not – the use of guards instead of if-else statements makes code look like real, pencil-and-paper math
  • Infinite lists – didn’t think that was possible?  Thank laziness.

Sold?  If you’re interested in learning the language, I would start with Learn You a Haskell, which gives a great rundown of all the important library functions, as well as gives you a good sense of how to use the functional constructs of Haskell.  More advanced programmers may want to look here.

Leave a comment

Posted by on February 14, 2012 in General, Programming


Tags: , , , , , , , , ,

New Year’s Resolutions

Since classes start tomorrow, I figured that now would be a really good time to make a post. I am more excited for this coming term than any so far while at Yale, in large part because I am finally going to get to dive into the heart of the computer science major and otherwise submerse myself in the computer science community. In terms of CS classes, I will be taking two: Data Structures and Programming Techniques and Design and Analysis of Algorithms. The first one is going to really whip my C into shape, and obviously give me forwards and backwards knowledge of linked lists, I mean, data structures (okay, okay, it’s really hard to be funny on paper).  The second course, a bit more theoretical and a bit more math-y, will definitely structure my thinking and approaches to problem solving.

But enough of the boring stuff.  I decided last semester that I love computer programming, and even more so, that I want to put the time in to be good at it.  Not, not good.  Great.  So over break, I spent a fair amount of time reading up in the Stanford CS Library as well as various programming blogs like Joel on Software, Steve Yegge’s Rants, and Coding Horror, and my reading prompted me to make the following New Year’s Resolutions:

  1. I will use version control.  Not just for CS assignments, but for papers as well.  As both Joel Spolsky (Fog Creek, Stack Overflow) and the guys at Reliscore described, the use of version control is one of the things that sets apart the good programmers from the great ones.  I can’t tell you how many times I made copies of my project folder for my Yale Daily News iPhone app — I realized that I needed to do version control, but at that point, hadn’t bothered to learn to use git (or even XCode’s built in repository system, for that matter).  But now, I am convinced.  So after a fair amount of research, I installed git on my computer, and set up an account with BitBucket to handle my online repositories.  Why BitBucket, you ask, rather than Github or Google Project Hosting?  Simple answer: free, unlimited repositories (public and private), and, if you’re a student, unlimited users.  BitBucket also allows you to keep Mercurial repositories, which is nice if you prefer that to git.  Also, for Mac users, SourceTree, made by the company who runs BitBucket, is a brilliant GUI for Git and Mercurial on Mac OSX.  Although I will make an effort to learn how to use git from the command line, SourceTree will really help to keep myself organized.
  2. I will learn how to use and write test scripts for my assignments.  In a technical interview I had the other day, I was asked not only how to optimize an algorithm for a given problem, but also what test cases I would implement to check that algorithm.  I was able to come up with a few on the spot — EOF conditions, whitespace, non-ASCII characters, int overflow errors, null pointers — but developing test cases on a regular basis will challenge me to think in a more complete way about the problems that I am solving.  I also decided that I will write my test scripts in Python, which, quite obviously, will help me learn Python.
  3. I will learn as much Linux as possible.  There are so many ways in which Linux can help with coding, whether they be command-line utilities like grep and sed or software like emacs (yes, I know I can use it on a Mac, but I much prefer TextWrangler) and valgrind.  If I want to consider myself a real programmer, then Linux is a must.

Those are my resolutions.  I’ll let you know how they go!

Leave a comment

Posted by on January 8, 2012 in General, Programming


Tags: , , , , , , , , , , , , , ,

Linked Lists and Pointers – struct node**

As I was reading up on linked lists is C and C++, I challenged myself to write an implementation for the data structure itself (i.e. the nodes) and the list operations (push, pop, etc). On my first go, I wrote the following:

struct node
    int data;
    struct node* next;

void push (struct node* head, int data)
    struct node* newNode = malloc(sizeof(struct node));

    newNode->data = data;
    newNode->next = head;

    head = newNode;

The node implementation is correct, of course, but the implementation for push is wrong. Any guesses?

Here is the correct implementation for push:

void push (struct node** head, int data)
    struct node* newNode = malloc(sizeof(struct node));

    newNode->data = data;
    newNode->next = *head;

    *head = newNode;

My guess would be that your first question upon reading this code is: “What is this struct node** business?” Well, that was my question, and it took me a few articles to figure it out. I first came across the problem in a programming interview book, Programming Interviews Exposed, but it took me until reading Nick Parlante’s article on Linked Lists in the Stanford CS Library to really understand it. I’ll save you the trouble of reading the whole document (although it is worth a read), but the gist of it is this.  When you pass a parameter to a function, whether it’s a primitive, composite (array), struct, or pointer, the function allocates its own stack memory for each of the parameters upon the function call, just as it does for any other local variable.  What this means for our purposes is that the first (incorrect) implementation of push only changes the value of the local variable head and not the value of the pointer that you originally passed, because in this case, head is a pointer to the struct data, not a pointer to the pointer-to-struct.  If you instead pass struct node** head, head then is a pointer to the pointer-to-struct.  By dereferencing head, you then can change the memory address stored at that memory location.

Moral of the story?  Pass a pointer to the data typeof the data that you want to change.  So if that’s an int, pass an int*, if that’s a pointer-to-int, pass an int**.  Seems easy enough…hope I follow my own advice!

Leave a comment

Posted by on January 2, 2012 in General, Programming


Tags: , , , , , , , , , ,

Pointers: A Five Minute Primer

Whenever anyone thinks about C, the first thing that comes to mind is pointers. In order to harness the full capabilities of what is a syntactically minimal programming language, you have to understand how C handles memory management, and how you can actually manipulate computer memory using C constructs. Here are the essentials:

What is a pointer?

The most basic way to deal with data in a function is to declare a variable and then initialize it with a value. For example, in the following case:

int x = 5;

We can think of the data value 5 being stored directly in a slot in memory named “x.” In the case of a pointer, however, the memory slot does not directly store the data. Instead it stores a memory address at which we can find the data. So if we look in slot “x”, we will find something to the effect of 0x714ae023, a memory address (in hexadecimal).

How to declare a pointer

Building on our previous example, let’s say you want a pointer to an integer (pointer-to-int). You would declare it as follows:

int* myIntPointer;

Thus the variable x is not of type int, but of type int*, a pointer-to-int (note you can also write int *x, but the former is better practice because it is less confusing; more on that in a second).

Pointer dereferencing

After you have declared a pointer variable, you might be tempted to say something like this:

myIntPointer = 5;

While the above is valid C code and will compile, it does not have the desired effect. What you have to remember is that myIntPointer stores a memory address. So rather than storing the data value 5, the computer (and your program) will think you are storing the memory address 5, and will look at that address for the data value.

So what do you do instead? In order to access and set the data stored at the memory address pointed to by your pointer, you use *, the pointer dereference operator. So in order to store the data value 5 at the address pointed to by your pointer, you would do this:

*myIntPointer = 5;

But wait, that’s still not correct…yet. Although you have declared the pointer variable (and thus the memory in which to store the address itself), you have not yet allocated memory in which to store the data value. When you first initialize. You need to use malloc, which is the C function that allocates a chunk of memory of the desired size and returns the address of that memory. It looks like this:

int* myIntPointer;
myIntPointer = malloc(sizeof(int));
*myIntPointer = 5;

One more thing to know. Any memory you have malloc-ed will not be reuseable by your program unless you explicitly tell your program to get rid of the data (see the discussion of stack vs. heap memory). So once you are done using a pointer and the relevant memory, you need to call free:

//code that uses myIntPointer


And that’s it! Then your program will be able to utilize the memory for another purpose, perhaps when you malloc memory for other data.

Aside: Pointers and structs

Let’s say you have the following struct declaration and intializations:

struct widget
    char name[20];
    double price;

int main()
    //random source code

    struct widget baseball = {"baseball", 9.99};
    struct widget* football = {"football", 19.90};

Normally, if you have a struct variable, as in the case of the ‘baseball,’ you use the dot (.) operator to access/change the name and price properties of the widget as follows:

    baseball.price = 5.00;

However, when you have a pointer-to-struct, you use the pointer dereference operator (->), as follows:

    printf("%s", football->name);
    football->price = 10.00;

How does C manage memory, and where do pointers fit in?

In C, there are two types of memory, local (or stack) memory and heap memory. Whenever a function is first called, allocates memory for all its variables in an area of memory called the stack. It is called the stack because every time a new function is called, your program allocates memory “on top” of the previous memory, and reads from that memory until the called function terminates. At that point, the program, having maintained a pointer to the calling function, will “pop” the memory for the called function off the top of the stack (i.e. deallocate it) and return to the previous function. That’s why if you declare a variable and store data to it within a function, the data is inaccessible to other functions once that function has terminated.

So how do we avoid this constraint? This is where heap memory comes in. Heap memory is a separate block of memory accessible to your program, but it is only used when you as a programmer explicitly decide to use it. In C, this would take the form of a malloc call, in C++, by use of the keyword new, and in Objective-C, with the message alloc sent to the classname. This provides you with memory that will persist beyond the lifetime of the function in which it is allocated; memory in the heap is not subject to the same lifetime constraints of stack memory. Thus, by maintaining a pointer to the malloc-ed memory, you can access and manipulate the stored data across multiple functions.

Think you understand pointers? Then here’s your first quiz: Knowing what you now know about memory management in C, what is the conceptual difference between:

struct widget myWidget; = "baseball";
myWidget.price = 10.0;


struct widget* myWidget = malloc(sizeof(struct widget));
myWidget->name = "baseball";
myWidget->price = 10.0

The answer, if you understand the stack and heap memory, is actually quite simple. The memory for the first struct is allocated upon function call from the stack, so the data persists only the length of the function. As long as the function has not finished executing (e.g. you are currently executing a called function), you can access and manipulate the data all you want. However, as soon as the function terminates, so too does the data.

The second version, in which the function maintains a pointer to the data, is different because the data is stored in memory allocated from the heap. This data will persist even after the function terminates, allowing it to be accessible (as long as you have a pointer to it) until you deallocate the memory with free.

If you’re looking for more help, check out Nick Parlante’s guide to pointers in the Stanford CS Library. And of course, if you have any questions for me, leave a comment! I’d be glad to help.

Leave a comment

Posted by on December 26, 2011 in General, Programming


Tags: , , , , , , , , ,

iPhone Programming, Part II

Alrighty, for those of you on the edge of your seats, here’s the rest of the story (see Part I here):

After doing a quick brush up on Objective-C (a la Apple Documentation, etc), the first thing I did was purchase a copy of the Big Nerd Ranch Guide to iPhone Programming.  I am a huge fan of programming books that teach by allowing you to work through a single project over multiple chapters, because they really helps to see how each of the concepts/features you learn might fit into a larger whole.  Years ago, I had purchased a number of books from the Head First series  (specifically, Java and HTML/CSS), and I found them to be phenomenal, particularly as someone who at the time had no prior coding experience.  Some might find the books a bit “immature,” and indeed, they are no replacement for a reference books or a more heavyweight books such as Steven Prata’s C++ Primer Plus, but for someone just starting out, they would definitely be at the top of my list.

Back to my project.  After working through the first 10 chapters or so, I switched to skimming the parts that would be relevant to my app, and skipping those that weren’t (e.g. iPhone compatibility, accelerometer, location services, etc).  Then, I hit the drawing board.  My contact at the YDN had given me a few general functional specs, but the implementation was up to me.  After drawing out a few diagrams under the Model-View-Controller paradigm, I was ready to go. Basically, I needed the following:

  • An XML parser – complements of TBXML
  • A list of articles – and new story object
  • A list of favorites – and corresponding singleton object
  • An interface

I chose to implement the XML parsing using a DOM-based parser – which, rather than notifying the program when it finds a new XML tag, creates a sort of “document map” that allows you to search for items based on various tags.  Not only does TBXML store item tags, it also saves the attributes of each xml tag — so, for example, I needed to get the link for an image, I could ask for the src attribute of the image tag.

The list of articles and favorites were pretty easy to implement.  The natural way to store all of the articles was in a dynamic array, which, in the case of Objective-C means an NSMutableArray.  In the first go around, I had them written as separate classes, because the while they are similar in concept – presenting a view based on articles, the implementations were very different.  Eventually, I decided to superclass them (I know, this is supposed to happen the other way; first make a class, then subclass it) with what amounts to an abstract base class in Java or simply a normal base class in C++/Objective-C.  Actually writing my own class structure helped me understand inheritance in a way that I had previously not.  The key lessons here are:

  1. figuring out which methods would be identical among subclasses
  2. figuring out how to write code that could be extended
  3. learning when to declare [abstract] methods – cases when I knew the subclass implementations would differ (e.g. in my case, filling the arrays with data) but wanted ensure that my subclasses would implement them.

Assembling the interface was my true introduction to iPhone programming and more importantly, open source code.  I won’t get into the nitty-gritty of UITableViewControllers and the like, but my discovery of open source code is worth mentioning.  Open source code is a phenomenal way not only to learn what good code looks like, but also to understand the role software libraries play in development.  Oh yea, and if used properly, your software can end up ten times more feature-rich.  Credits here to:

  • Enormego for PullToRefresh and Lazy Image Loading (uses a cache)
  • Tom Irving for TISwipeableTableViewCells (like the twitter app; reveals a set of buttons behind a cell)
  • Facebook for their SDK and sample implementation
  • Sharekit for showing me how to email a link and post to Facebook or Twitter from within an app

Note: These last three did not make it in version 1 of the app (currently in the store).  I’m working on an update as we speak.

After I finally assembled my code, it was off to testing.  The hardest part about testing and debugging was (unsurprisingly) memory management.  While memory leaks/errors are difficult to find in C/C++, I feel that the rules for memory management are much more straightforward.  You don’t have any of the retain/release jargon concealing the basic facts about memory management in the C family of languages — memory is either allocated in the stack or in the heap (Note iOS 5+ makes Objective-C more similar to Java in its memory management with Automatic Reference Counting, although it still retains elements of its heritage). Best resource I found for finding memory leaks and understanding how to use the Leaks tool in Instruments (part of the developer software) was from Mobile Orchard.  Also, XCode has a feature you can enable called NSZombie, which will allow you to see if you are messaging a deallocated object.  If your program is crashing with a sigabrt, I would check this out.  Regardless, my recommendation: understand memory issues before embarking on a larger-scale project.  It will save you a lot of debug time in the end.

All told, it took me about six weeks to teach myself Objective-C and make the app and get approved by Apple, which was extraordinarily gratifying, both on an intellectual and personal level.  Hopefully, it will be the first of many!

Here are some screenshots of version 1.  You can download the app here:

YDN App Screenshot

YDN App Screenshot 2

Leave a comment

Posted by on December 21, 2011 in Programming, Projects


Tags: , , , , , , , , , ,