2014-09-04

Counting to 100

Today, while reading Chapter 6 of The Unix Programming Environment by Kernighan and Pike (Programming with Standard IO), I wanted to test a program that took a long-ish file as input. Naturally, I needed to have a long-ish file, and I don't generally keep them lying around, as I an near-obsessive about the organization of my files and folders and trash anything I won't need again. I opened up a new document in vim and decided that I wanted 100 lines of text to properly test the program. Okay then.

Like a true programmer, I wanted to make sure that no lines were being dropped, so I intended to number them. Also like a true programmer, I was too lazy to type out 100 line numbers by hand. While normally, when in such a scenario, I would try to learn [far more than] enough perl or awk or some other basic scripting language to perform this task, I had been reading about IO in Haskell only the day before.

Haskell, for those who are not familiar, is a functional programming language that can be described as "like lisp, but without the parentheses," or "like Haskell, but with a less recursive comparison." Being a functional programming language, Haskell is great for all sorts of things, with many mathematical applications. Granted, without optimization, Haskell can be much slower than other, less functional languages, but it can be useful sometimes.

As the task I faced involved counting to 100, the mathematical nature of Haskell lent itself to this problem. I wrote out the following line of code

main = sequence $ map print [1..100]

and entered

:.!runhaskell

into vim. This produced the numbers from 1 to 100, all on their own line, with some extra output at the end:

[(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),(),()]

but that was quickly dealt with. While I mostly view this application of programming knowledge as an achievement, I also feel somewhat of a false programmer, because my laziness actually expedited the result instead of impeding it by leading me on a wild goose-chase of discovery and mosquito-nuking.

This is my first post on my first real blog, so I welcome any comments/criticisms/heckling either related or wildly unrelated to this post or blog.

EOF

No comments:

Post a Comment