Having been using Python for a while, I am fully enamored of the language. It is elegant, easy to read, and wonderfully descriptive. The only drawback is a serious case of the slows, but I am hoping that this will be fixed eventually. When I think of all the time I spent writing Java, it is enough to make me cry.
Writing code in Python flows well because of the little touches:
help(module/function)
, python -i
, and others. When writing I
actually don’t have a reference manual or anything on the screen:
instead I just have a Python interpreter running in a editor window, and
test little snippets of code there. It flows well. As an example, for
the past two months we’ve been working on a middleware layer in Python
for a soon-to-be-released website. Total time spent coding? Typical
Silicon Valley hours. Total time spent in the debugger over the same
time period? Mmmm, probably 30 minutes. That counts both pdb.set_trace
calls and Emacs pdb mode.
Unbelievable? If you’re used to C++ or Java, it might be, a little. But being able to test code routines as soon as they are written, in conjunction with unit tests, means that I hardly spend any time in the debugger.
The Agony of Ocaml
I want to like Ocaml, I really do. Syntactically the language is a little quirky, with a mix of a bunch of different styles, but, hey, I programmed in Perl, so what’s a little heterogeneous style-mixing between friends? But even better, I can code something really quickly in Ocaml imperative style while I would still be figuring out how to elegantly do it in Haskell.
And that language is fast. I mean, really astoundingly fast, which is why it is so evil. It’s like that psychotic girlfriend that was amazing in bed. You keep trying to break up with her, but she seduces you right back into that abusive relationship.
Ocaml has that seductive speed, it’s got an interpretive toplevel, so it makes you think that it’s a really cool language. But then it punishes you, again and again. If Python is an honest and open relationship where everything is on the table, Ocaml is a sulking, pouting one that makes you go through pain and punishment to figure out what you did wrong.
Oh, you want an example? Try Python’s
>> a = {} >> a['x'] = 10 >> a['y'] = 20 >> print a {'y': 20, 'x':10}vs. Ocaml’s
# let a = Hashtbl.create 100;; # Hashtbl.add a "x" 10;; # Hashtbl.add a "y" 20;; # ... how do I print!? ... # let hashstrint_print = Hashtbl.iter (fun key data -> Printf.printf "%s: %d; " key data);; # hashstrint_print a;;Ignore the trailing semicolons and spaces, please don’t make me fix that. I have to write one routine like this for every combination of key/data hash table that I have. There isn’t even a printing facility for lists, you have to use List.iter and the right print for your data types. Traces show <poly> for anything more complex than a simple type. The debugger continues the brain dead approach to values, giving you the same (lack of) facilities as the toplevel. It makes Ocaml so frustrating: it’s wonderfully expressive, it’s faster than greased rockets, but it’s impossible to see your data while debugging!
Maybe it’s a requirement that functional languages make it painful to get output, something that is built into the universe at a basic level. Ocaml has their strongly-typed and non-overloaded print routines, while Haskell has monads. (Whoops, forgot about Lisp/Scheme. Once again Lisp is ahead of the curve).
There are some options, kinda. Metaocaml allegedly has a polymorphic
print, but I haven’t used it. You can use higher level functions to make
printing stuff easier, as in
let hash_print format = Hash.iter (fun key data -> Printf.printf format key data)
,
but that only works for simple types that can be handled by a printf
format specifier. There is the extended library for
Ocaml,
which, if you ignore the tar file errors, has a dump/print routine that
works okay.
I would like to propose my Eighth Law Of Coding, which should be:
If your language doesn’t have a polymorphic print, there is something wrong with the implementation or the language, or both.
Yes, yes, I know — you can write your own printers and attach them to the toplevel/debugger. To that, I say: Why, thank you, Ocaml, thank you for allowing me to code my own routines to print a hash table.
Comments are moderated whenever I remember that I have a blog.