September 25th, 2006, 8:14 am
QuoteOriginally posted by: mjhmm, i wonder what C++ book you have in mindWorth bearing in mind that that code is written as much for paedogogical purposes as anything. It's also written largely to facilitate extensibility.A better measure might be how much time does it take to add a new pay-off type?Actually, for the examples in mj's book, adding a new payoff in Ocaml is very simple. Adding a new instrument is a one line change. Say you have this datatype definition:type instrument = | Forward of float (* strike *) | ... ( plus cases for american option, european option, etc)This is an instance of "is-a", which you encode with inheritance in OO. In functional languages, you use disjoint sums to encode it. You add another case (say digitals) by extending the type definition like this |... | Double_digital of float * float (* lower, upper *)This definition is concise, and I claim that is readable. Now you need to define functions on that data, or extend exisiting ones. For instance, the payoff function: let payoff (instrument, spot) = match instrument with | Forward strike -> spot - strike | ...Here 'match' is doing pattern matching on the instrument type. You add antoher case, by adding a new pattern: | Double digital (lower, uppper) -> if (lower <= spot & spot >= upper) then 1.0 else 0.0It's seldom the case that you can replace a whole C++ class (.h and .c++ files) by such little Ocaml code, of course. However, I have looked at the C++ code in Quantlib, and I claim that it would be possible to write a better version in Ocaml. Reducing code lenght is not the goal, of course. Reducing the time and money it costs to develop the code is what is important. Ocaml is a general-purpose language; if your problem can be solved with a domain-specific language (say Mathematica, Matlab, ...), and it meets your performance goals, then it'll be cheaper to use that.