This "Larger Example" I’m going to write about is in chapter 18, starting on page 243. The chapter is (supposed to be) about the object oriented features of OCaml (they would be the "O" part) but this example is more objectionable than objective. It was intended to be an implementation of the Levenshtein distance algorithm: it started with an incomplete description of that algorithm and a figure of a matrix with four mistakes. Then it got worse.
Smith’s plan was to create a base class containing the algorithm and then use subclasses to adapt the algorithm to different data types. But when I looked at the edit_distance base class I noticed that half of the core algorithm was missing! I found it in the string_edit_distance subclass, and also in the list_edit_distance subclass. Martin Fowler in his great "Refactoring" book says: Number one in the stink parade is duplicated code. If you see the same code structure in more than one place, you can be sure that your program will be better if you find a way to unify them.
Following Fowler’s good advice, which Smith should have done, I moved the duplicated code to the base class. There was only a small bit of code that was different for each subclass: the part that compares the elements of the particular data structures (strings and lists in this example) to work out if a changing cost is required. I created a cost_to_change method to handle that.
Then I tried to simplify the code and make it more readable.
In the update_matrix method I removed three of the let bindings and passed the calculation results directly the trimin function I mentioned before.
In the gen_matrix method I replaced the pattern matching and nested iteration functions with two separate iteration functions, which hopefully makes it clear that we are only initialising the first row and first column; most of the matrix is left untouched.
In the calc method, which I’d moved from the subclass to the base class, I again replaced the pattern matching and nested iteration functions, this time with nested for loops. Pattern matching and higher-order iteration functions are very powerful, and they could be used instead of loops and conditional statements in all ocaml code; but ocaml includes for loops and if statements because they are easier to use and understand than the more powerful features. Choose the right tool for the job.
As a result of all these changes the size of a subclass has been reduced from 18 lines to 6, only 2 of which need to be changed for each data type (the other 4 are structure). You can have a look at some of Smith’s code and my modified version to see the differences for yourself.
I was about to comment on the "Larger Example" in chapter 18, starting on page 243; but first I have to get rid of the trimin method it contains:
method private trimin x y z = match x,y,z with
m,n,o when (m > n) -> if (n < o) then n else o
| m,n,o when (m < n) -> if (m < o) then m else o
| m,n,o -> if (n < o) then n else o
Let me now repeat exactly what I said in my previous post: Huh?! This method will return the smallest of its three arguments, although its ugliness made me doubt that for a while. But even if it were improved, it shouldn’t even be part of this class at all. So I removed that method and replaced it with a simple function:
let trimin x y z = min x (min y z);;
That done, I was then able to continue with the "larger example".
I have been reading Practical OCaml. I didn’t buy the book; I recommend that you don’t buy it too. I was given a copy and asked if I could say something good about it, so here goes: “it could be worse”.
There are so many other negative reviews of this book I don’t want to just repeat what has already been said. Instead I’m going to criticise some parts of it in more detail in the hope that it may help. I’m going to choose targets on a whim, with not much rhyme or reason; that should mirror the structure of the book quite well.
So, to start small, my first complaint is against page 172. Smith (the book’s author) writes:
let min x y = match x with
n when x < y -> x
| _ -> y;;
Huh?!
That can’t possibly be a sane way to define a function that returns the smaller of two arguments. In addition to the needless complexity we have the unused named value n. What was wrong with:
let min x y = if x < y then x else y;;
That should be clear enough. But I now have another question: why even bother? The min function is built-in to OCaml; it is part of the Pervasives module that provides “<” and the other comparison operators.
Published in Uncategorized
.
They must really want to make it easy for us to attend YAPC::Asia this year: the venue is beside our apartment!
Published in Uncategorized
.
In one of my Japanese classes I was studying transitivity pairs of verbs: in each pair both verbs have the same basic meaning, but one is transitive and the other is intransitive. (A transitive verb requires an object; an intransitive verb does not.) For example, one of the pairs was 「出す/出る」, which means “take out / go out”: 「ごみを出す」 means “take out the trash”; 「出る」 means “I’m leaving”.
The textbook then described how the meanings would change in the present imperfect tense (the book didn’t call it that; it just called it the 「+ている」 form). Transitive verbs describe an action, so the present imperfect tense of a transitive verb describes an action in progress; this is a normal use in English. But intransitive verbs describe a change, so the present imperfect tense of a intransitive verb describes a state that is the result of the change. That made sense, and I hope it still does.
But then came the example sentences. They are usually good examples that can be used in normal conversations, so I wasn’t surprised by the intransitive imperfect for “break”: 「このコンピューターは壊れています。」, meaning “this computer is broken”, is a useful phrase. But the corresponding transitive example was 「ゴジラが町を壊しています。」 translated as “There goes Godzilla, destroying the city.”
I’ve just been surprised and amused by an animated picture designed to identify the dominant side of your brain. (I’m not going to post the image here as I cannot find any copyright details for it. You can find it on many websites, including Gizmo Watch and Facebook.) The picture shows a silhouetted dancer spinning, but the direction of spin, clockwise or anticlockwise, depends on your brain.
When I first saw the picture she was spinning clockwise, but when I started to read the text she immediately started spinning anticlockwise! That makes perfect sense now, but it surprised me when it first happened; and it still amuses me every time I repeat it.
The explanation for the change in spin is quite simple: when I first look at the picture the right side of my brain tells me that she is spinning clockwise; but the left side of my brain (and your brain too) is responsible for reading, so when I start reading the left side kicks in and tells me that she is spinning anticlockwise; when I stop reading, she starts spinning clockwise again.