?

Log in

No account? Create an account

Ocaml

« previous entry | next entry »
Nov. 2nd, 2005 | 12:15 am

Last weekend in amongst gaming and pumpkin carving parties and rides on the metro system, I managed to learn (at least the basics of) OCaml, a strongly typed functional programming language.

Whee after about 6 hours of reading and a bit of practice I was able to write functions to compute the minimum of a list or read a file.

One of the things I found most interesting was the performance of my extra-ordinarily bad benchmark. One of the easiest things to do in a programming language is tell the computer to count to (or from) a very large number. Since ocaml provides an interpreter, a bytecode interpreter, and native code generation I had to see how my tail recursive function worked. The drop from 10-ish seconds to .5ish seconds was quite impressive.

Then I compared it to python.
for x in xrange(300000000):
  pass
took about 30 seconds
x=300000000
while x > 0:
  x -= 1
took about 80 seconds. Python's integer updates are slow

Link | Leave a comment | Share

Comments {3}

TroyToy

(no subject)

from: troyworks
date: Nov. 2nd, 2005 08:31 am (UTC)
Link

interesting to hear you impressions I've been wondering what Ocaml is good for.

Reply | Thread

Diane Trout

(no subject)

from: alienghic
date: Nov. 3rd, 2005 10:44 pm (UTC)
Link

The oddest thing about ocaml that I've noticed is the utter lack of operator overloading. To make the type inference system work, even the + is not overloaded. "+" by itself is for integers, to add floats you need to use "+.", and if you want to concatinate two strings you use ^.

On the other hand, the language is fairly simple, it tends to perform about the same as C++, but provides garbage collection, and comprensible list processing functions.

Apparently in modern C++ (taken from a swig example) you can do something like
void halve_in_place(std::vector& v) {
    std::transform(v.begin(),v.end(),v.begin(),
                   std::bind2nd(std::divides(),2.0));
}
It looks like std::bind2nd(func, val) will make a new function from divides with the second argument bound to 2.0

I think
let l = [1.0;2.0;3.0] in
let div_by_2 x = x /. 2.0 in
List.map (Printf.printf "%.2f ") (List.map div_by_2 l);;
is more comprehensible than the C++ version.

I'm hoping that ocaml is a better choice for writing code to run on embedded xscale processors than python, without being as complex to code as C/C++.

Reply | Parent | Thread

Diane Trout

(no subject)

from: alienghic
date: Nov. 3rd, 2005 10:45 pm (UTC)
Link

Oh yeah I completely forgot to mention The newest great computer shootout which is where I got the claim that ocaml performans similarly to g++.

Reply | Parent | Thread