|
|
|
|
|
by ufo
4115 days ago
|
|
The ocaml version of this code would be actually quite similar, since you can even use imperative hash tables if you want. The syntax is a bit fugly and the stdlib doesn't have some of the helpers python has but I wouldn't go as far as say that its "horribly ugly". Beauty is in the eye of the beholder and Ocaml has the advantage of being much less "magical" than python (as per the "explicit is better than implicit" mantra) * In ocaml you need to pass some "comparator" parameters around if you want top_k to be polymorphic. Its similar to haskell type classes but you need to be explicit...
* Instantiating a polymorphic hash table is a bit ugly but thats more about the standard library...
* There is no "default dict" function so you would need to initialize the counts yourself
* Looping the file is not hard. Most data structures will have some sort of "fold" or "iter" function you can use.
* The sorting function doesn't take keys, so you need to use a comparator instead. Its not that bad though - the function to convert a hash table into a list already returns a list of tuples. |
|
It's conceptually similar, but just plain uglier in practice. A lot of day to day programming is just FILLED with stuff that is very elegant in Python, and not so much in OCaml.
Language engineering is something is very elegant in OCaml.
In my opinion, EVERY language is a DSL. I stopped looking for the ultimate language -- it doesn't exist.