My (First) OCaml Program

I spent a long time doing not much, reading occasionally; then too long fighting w/ syntax (long story short, parentheses don’t behave like I’d expect — or more precisely, operators require parens for arguments where I wouldn’t think it would matter…)

Anyway, here is my code (as a Word doc, with notes) — feel free to comment.

Update before I even posted — I found a bug.

Here is the revised code (see p. 2) — the function find_a_b is asymmetrical; that is, “half way” thru the list of factors (at which pt, normally a*b = b*a, isn’t at the sqrt of n…)  That’s a very poor description, but if you want the full math, let me know and I’ll elaborate (in a new post).

Now on to the interesting project.  I finally did what I set out to do.

Here is the result — view that PDF while reading my commentary here:

lns. 11-15 create a Map structure (integer based index) — not an array;
l. 17-18 define the input/result — I was having some problems w/ input parameters and return value(s), so I switched to this structure; I could convert back now that I know what I’m doing, but it was easiest to leave it as is…

l. 29-59 are the first implementation — it works, it does what I want, but has much debugging and offered a couple of easy improvements
l. 61-62 are a first attempt, and I examined the result to see I was on the right track (see also the commented out section l. 73-87
l. 64-65, 58-71 define two functions for displaying the result(s) and aiding in debugging

l. 97-122 give a second definition, with some debugging commented out, and primarily move the base-case “up” so I only need one conditional, and I also remove old elts from the map as I “use them up.”

Lines 145-170 present the “final” version of the function — which very closely matches the Haskell code in the paper that was the “inspiration” for me learning OCaml to begin with…

Lines 173-186 are the code that runs for a million, then 1.2 million following that (and shows how to “loop”).

Riddle me this: the “compiled” code (ocamlc.exe produced byte-code executable) runs for two million, but gives a stack overflow at 2.1 * 10^6 (after two hours of run time).  The optimizing compiler [ocamlc.opt.exe] produced slower code that overflowed the stack much sooner…  I don’t get it.  (Besides, shouldn’t this func. be tail-recursive and, as such, never overflow the stack??)

Finally, lines 188- present a final function which worked up to 1 billion (I executed w/ count=100,000 ten thousand times — I see I changed to 50,000 in the print-out; but, if you want proof, the resulting trace is a half-gig text file, but it compresses to 83 MB!).

Having done what I set out to do, I’ve grown bored w/ OCaml.  I may do more later, but for now, I’m on to Clojure, because it’s Lisp-like, JVM-based, and functional/multi-threading friendly.

If you have an interesting problem for me to work on, please let me know.

Someone suggested a comparison between various programming languages’ treatment of operators (@kaleidic on twitter, but he also programs in J, which is also interesting…)


One Response to “My (First) OCaml Program”

  1. Customer Service — good and bad « More than a Tweet Says:

    […] I wrote this almost a year ago, now, and it gets the most visits (I’ve written more: about functional programming, and even another customer service story); maybe I need to mention high profiles search keywords […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: