Archive for the 'Code and Consequences' Category

Taking Another Stab At Learning Haskell…

Sunday, March 22nd, 2009

I have been trying to learn functional programming in Haskell on and off for about two years now. The functional programming approach, terminologies and Haskell’s unfamiliar syntax makes me feel like a fish out of water every time I try to learn it. What makes it especially difficult for me is that the BSCS course I took did not have a functional programming subject plus the fact that I a mostly self-taught. I first learned to program using Atari Basic and 6502 assembly language. From then I learned C, Pascal, C++, Java, PHP, Perl, Python, and a slew of other esoteric languages. I also know CLISP and ELISP so I have some familiarity with functional programming. One thing that all these languages had in common is that they are all procedural, imperative programming languages. I have been conditioned into the mindset that a computer program is a series of instructions given to a computer. Learning functional programming and, at its heart, lambda calculus throws most of what I know about programming out the window. You are forced to think differently. Programs are no longer a series of instructions. They are now special applications of fundamental and advanced mathematical concepts.

Most of the material I have been reading so far introduces concepts which are completely foreign to me using terms that I have never even heard before (”monad”, hyukhyuk!). Some materials like “Haskell Tutorial for C Programmers” attempts to map functional programming concepts in Haskell to imperative programming concepts in C. That was my last attempt to learn Haskell prior to this current one. It starts off slow but in the next chapter it gets deep all of a sudden and I felt lost and eventually gave up.

The approach taken by “Haskell Tutorial for C Programmers” is essentially the same approach that I had taken to learn all the programming languages that I know. This approach consists of three stages:

  1. Learn about all the keywords of the new language, then map them to the keywords of a language that you already know.
  2. Learn how to do all the concepts using the new language like loops, branching, function definition, etc.
  3. Finally, learn how to do advanced I/O like reading/writing files and talking to a network.

This approach has not failed me until I tried to learn functional programming using Haskell. I eventually gave up until better material was available and I had some spare time. I have been especially productive using Django and Python lately so I have been finding a bit of time and with a new book on Haskell available for free, I decided to give it another shot.

I am reading well into chapter 3 as I write this and so far, my brain is still chugging along nicely. I took a completely different approach to learning this time: Zen Mind, Beginner’s Mind. It is at the core of the teachings of Shunryu Suzuki-roshi. Basically it means emptying your mind of all preconceived notions and being free from expectations of what should and shouldn’t be. In this mental state, all you do is explore and observe.

The approach is working thus far. However it would not be possible if “Real World Haskell” itself was written in the same cut-and-dry approach like that found in most of the books on Haskell that I have read. The book takes on a very nice pace in the first 2 chapters so far. It introduces functional programming concepts using small, working, real-world examples. It exposes only as much of the language as it needs to and explains everything in detail without overwhelming you. Considering the depth of the subject, it looks like an excellent book written in a very engaging manner.

Django File Upload Handling Examples

Wednesday, February 18th, 2009

I have been working on a multi-user blogging and publishing platform using Django 1.0 lately. Naturally this requires the backend to be able to handle file uploads. A lot of things have changed from Django 0.96 which I am still using on some legacy code. One of those changes is the way Django 1.0 handles file uploads. Most of the changes done were made to allow Django apps to handle large files without soaking up too much memory.

So what has changed? The most visible change is that there are now at least two separate API’s that you have to work with. You have the File API and the Storage API. The File API which exposes the File class provides a thin wrapper around Python file objects. The Storage API, on the other hand, exposes a base class Storage that you can use to implement custom storage facilities. There is another API that provides FileUploadHandler. This will allow you to customize the way Django handles the uploaded files in their “raw” form. For most purposes, the File and Storage API will suffice.

This post is meant to supplement the information found in the “Handling Uploaded Files” section of the Django File Uploads documentation. You will still need to refer to documentation.

(more…)

Writing a Facebook App using Django

Wednesday, December 31st, 2008

I recently found myself writing my first Facebook app using Python with the Django web framework. I have to admit that it was not a very pleasant experience. The usual Django development cycle of programming and testing locally does not work. I have a very simple workflow nailed down when writing Django apps: write, test locally, commit to VCS, and upload to server. When writing a Facebook app, I had to change the current workflow a bit: write, commit to VCS, upload to server, test on production.

Facebook, being a PHP-powered site, likes to make the assumption that you will also be writing your app using PHP. They don’t even make any efforts to hide their bias towards PHP. The “official” client library is for PHP. The example program they give to you the first time you sign up is written in PHP. All the relevant starting points all lead towards PHP-centric development.

Thankfully, there are those who realize that PHP is not the be-all and end-all to programming web applications. You can find unofficial client libraries for other programming languages. Download and install the client library for your programming language of choice and follow the documentation and hopefully you get to have everything working on your first try.

(more…)

Ditching PHP: Alternatives to PHP

Wednesday, November 26th, 2008

You have worked around the quirks and you have lived with the inconsistencies. You have stood by and watched your code fail spectacularly between minor point releases. You scrambled to fix your code’s dependence on register_globals set to on when they got turned off by default in version 4.2.0. You have lived with the disappointment when version 5 failed to deliver on better object orientation. There have been a lot of warning signs telling you to look for better alternatives, but still you persevered. Then came the recent changes that really make you shake your head in disbelief. Face it, PHP’s direction as of late has really taken a downturn. You’re thinking it’s time to look for alternatives.

If you are one of those folks who have been using PHP and nothing but PHP for a long time you will be hard pressed to find an alternative. PHP is, after all, one of those things that are “easy to get into, but difficult to get out of”. PHP lures you in with its forgiving nature, vast library of pre-built extensions, ease of deployment and just plain “instant gratification”. It’s easy to forget that there are other languages for server-side programming/scripting.

(more…)