Why did you learn OCaml?

I heard that computational expressions are not technically the same as do notation, because they integrate both comprehension syntax and do notation. I don’t know is that enough to say that CE are more powerful than do notation.

But all of that is offtopic, of course.

[1] https://github.com/dsyme/fsharp-presentations/blob/master/design-notes/ces-compared.md

P.S. Maybe ocaml vs haskell posts should be rebased to the new orphan thread too…

3 Likes

There are many things in your very poetic post that I do not fully understand, but I get a lot of what you mean. Personally, I have no formal background in maths (I’ve never even taken calculus), but I also find that modelling computation in terms expressions makes it so much easier to think about programs. Imperative programming sort of feels like throwing s*** at a wall to see what sticks, and statically typed functional programming feels like… some kind of mechanism where things “click” into place once they are correct. I like the click.

1 Like

I’m not sure about that. I think the question “why did you learn language x?” implies “instead of learning/sticking with language y”.

When OCaml is x, I think the most frequent cases of y are going to be Haskell, Lisp or F#. Of the three—in my not-particulary-humble opinion—Haskell is the only “serious competition” to OCaml. I think it makes a lot of sense to go into details about their differences in a discussion about why someone uses OCaml.

(and this is not to say that one should not use both Haskell and OCaml. They are both great programming languages)

[1] The paper I referred to was «The F# Computation Expression Zoo» http://tomasp.net/academic/papers/computation-zoo/computation-zoo.pdf

Thanks for the link to Don Syme’s discussion.

1 Like

So to also get back on the original topic:

After some Basic, Pascal, Maple, Mathematica, and 90’s Casio/TI calculator programming, my intro-CS class in Engineering School was using Caml-Light (which is very good for teaching but leaves students with the false impression that functional programming is just for didactic toy-languages).

After that came a ton of Java, C, C++, assemblers, Matlab, …

First job after school, I wanted to see how people make software that actually works, so went into “embedded/safety-critical avionics” → tiny subset of C with less than 1% of the time spent programming very dumbed-down algorithms (instead, by very far, the fun part is writing tests: very complicated scenarios that make many crazy situations happen).

Then got bored, started PhD in multimedia/distributed-systems (2006). Still interested in making things work instead of seg-faulting/CVE-ing, so I got interested again in OCaml, and because it was for my PhD I could just start just using it, nobody cared — first I wrote “first-person/walk-through” 3D engine with LablGL(UT) → my own horrible pile of java-like code with a ton of objects and Option.get_exn's (= “ocaml.lang.NonePointerException” :)).

Fun to be the only statically-typed-FP person in a community were most people believe that somewhere on Earth, there exist programmers capable of handling things like C++ or dynamic typing and that those hypothetical superhumans are actually among them!

But what I was making worked & had good performance for a tenth of the effort, so :man_shrugging:.

Then some security, some biology/biomedical research software, and now cryptocurrencies, all mostly in OCaml, with music software as hobby programming too.

Over the years, sometimes frustrated with some OCaml quirks, I’ve taken a hard look at, learned, or tried every language that has a type system, I always come back to OCaml as by far the best compromise (and yes I have quite a bit of Haskell experience and I think type-classes are a toxic anti-feature ;)). Moreover, things that were annoying me for years have been fixed now by the community: a fast & hackable build system and decent automatic code formatting :pray:.

This is all very nice and shiny but The Future is OCaml becoming just glue code around Coq-extracted/generated code; so I hope I’ll get to use & write more real-world Coq as soon as possible (sorry for those who know me and have heard this a 100 times: go read/make-the-exercises of Software Foundations IMHO best book ever written on all of computer science).

8 Likes

Like anything there are always pro-and-cons I guess. It would be interesting to hear your perspective. Why do you think type-classes are a “toxic anti-feature” ?

This would be great in a separate (OT) thread perhaps.

1 Like

A bit late to the party but I’ll add my story.

I came to OCaml via the Real World OCaml book. Absolutely fantastic resource and looking forward to a second edition that I can point people at.

Previously I’d been working in Erlang, Javascript and Ruby professionally and was getting very frustrated with a large Javascript and Ruby codebase that I was migrating from one JS framework to another. The lack of types and help from the compilers was costing us a huge amount of time and causing many runtime bugs in our production system. I could see that a typed language could save us real money and headaches, so I started looking at OCaml and Haskell.

I’d previously had exposure to Haskell via Uni and the local functional programming group (FP-SYD) but I didn’t feel like I could sell Haskell to my colleagues. So went with OCaml as the more pragmatic option that I could support and explain to people, while still getting the core benefits of ADTs and type inference. Since then I’ve worked in both OCaml and Haskell professionally and really like both of them for different reasons.

In OCaml I really like the Object system and polymorphic variants, and the tooling ecosystem is absolutely fantastic (opam, dune, etc)

6 Likes

Reads like poetry! :slight_smile: Has the thought occurred to you that you might be more intuitive (or right-brain) than rational (or left-brain), to such a degree that you’ve come to “simulate” reason with powerful intuition? I’ve always been a math and science person, with mostly-ignored artistic talents which surface sometimes. I had trouble “showing my work” with computations, but was frustrated because I didn’t know how I was working.

Decades later I had developed a lot of insight into my own thinking, partly from work on videogame AI and using my own problem-solving as inspiration. And it dawned on me that I’m not very “rational and formulaic” at all. I don’t remember recipes – I remember ideas, so I always improvise. I “compute” by messy abstractions scattered across my thoughts being tested in parallel, and narrowing on ever more promising results. But the way I solve a similar problem can easily vary from one instance to the next… constant improvisation. I get a sense that my naturally-intuitive thinking has been hammered and molded toward “rational problems” like math and programming. My thinking and learning are quirky, but my talents are in innovating… which seems to fit with my theory of my own thinking.

Anyway, the way you described your language-quest and working with abstracted impressions seemed familiar.


My own discovery of OCaml was while working at a game studio… one of the fresh students was opposite me over a low cubicle wall. He mentioned one day that a language he’d love to get a chance to work with again was OCaml. “O-what!?” I’d never heard of it (this was 2004). He had one class with it at university (Stanford maybe?). He explained that it was functional but compiled, so I might like it. My style of C (and also C++, as much as it could be done at the time) was somewhat functional – preference for expressions over statements, and composition of functions rather than everything in objects. I’d just never considered a functional language because I thought they were interpreted (for me, in my ignorance, it always brought to mind Lisp).

Anyway, I took a rare “weekend off” to explore OCaml and… I’d already hated C++ and merely found C to be “acceptable” for what it does… now I couldn’t tolerate C++ at all. So frustrating. At every turn I found myself wanting ADTs and closures. Previously I’d done ugly boilerplate-heavy workarounds to simulate what closures offer automatically. Well, I was still doing that, because I was still working with C++03.

It took almost a year before I really fell for modules… which just meant another thing I now can’t live without in other languages. :stuck_out_tongue:

I watched the birth of Rust with hope… as an even lower-level performance-oriented “OCaml”. Then it became a better alternative to C++, which is really awesome… but not enticing enough to lure me from OCaml. Oh, I’d be totally happy working with Rust at a game studio. But OCaml has ruined me for most other languages.

4 Likes

Doesn’t everyone do it that way? :joy:

Me and @jerben have similar reason why we got to OCaml. Basically we got annoyed by many other languages when writing bigger systems (mainly writing web apps/services). Ruby, Python and JavaScript allow you to start fast, but quite soon after code starts to evolve, it gets hard to keep everything correctly wired. In JS world, we looked into Typescript, but it was simply too weak. Then @jerben went through quite big research to test different languages and frameworks. As result of this we came into conclusion that OCaml is the one to go for, even when we need to do something as crazy as writing our own web framework that lead us to start building Sihl. We would have end up writing much of the framework anyways, because none of the existing frameworks we providing the abstractions we needed. On the other hand lots of low level blumbing we could have had for free that we are missing in OCaml.

Basically our bet is that in the long run, having strong type system allows you to be faster when you spend less time in debugging your tests when something broke down after refactoring the code. And there’s also less test code to maintain. Only downside so far has been with OCaml ecosystem being small and we need to implement a lot things ourselves. Though I hope this is kind is a one time cost. Otherwise experience with OCaml has been really nice.

13 Likes

After migrating a project with @mikoni from JavaScript to TypeScript, we still still couldn’t handle the growth like we wanted to. Changing code was still too expensive. So we decided to migrate parts to Reason on Node, which lead to really nice results. The amount of bugs we introduced as we added new features were very low. That’s when we knew we found a great tool. After having a closer look at the OCaml web development story, we decided to ditch Node and to switch to Reason native. Shortly after we switched the syntax to OCaml as well.
We are still struggling with the state of the web development ecosystem from time to time, but all the nice things of OCaml make up for that easily. And this is not a problem that can’t be fixed! :slight_smile:

15 Likes

I’m not so sure about that—or at least I think that sets the bar pretty low for “high barrier to entry”. From where I sit, the main barrier to entry to OCaml is that the syntax is different from any popular language, but Python syntax is also different, and nobody thinks the barrier to entry is high there. What else is actually challenging in OCaml?

Variants? Just a tagged union.
High order functions? Most languages have them these days.
Module functors? Meh. If you know what a module is and what a high-order function is, it’s nothing special.

There are legitimately novel features in OCaml like polymorphic variants (which are not that hard) and GADTs (which were kind of hard for me to grok), but I you can pretty much avoid these topics as a beginner—GADTs for sure!

1 Like

I came to OCaml after Clojure first revealed the joy of functional programming to me, and later as a refugee from Elm when I wanted something in that vein but more flexible and something I could write general applications in. I’ve used OCaml for for a few years as a prototyping tool and for hobby projects, but I’ve recently had the good fortune to be able to use it for some significant projects at work. For me it strikes the perfect balance - it’s expressive, performs well and compiles quickly. I like how you can keep it simple, but the powerful things are there to be leveraged should you need them (functors, first class modules, the object system, GADTs etc).

I also like all things French so that probably biased me somewhat.

4 Likes

Ron Minsky in one of his talks said that because OCaml barrier of entry is high, it draws high quality candidates to Jane Street. :slight_smile:

Barrier of entry might not only have to do with the syntax. The fact that there are very few jobs and resources are scarce, requires the people who want to learn and use it to invest much more energy.

With respect to the syntax, most experienced developers come from OO languages. Even though there is O in OCaml, it’s not particularly encouraged. Variants? Again, OO folks were trained to create interfaces and derive classes from it as opposed to a big switch. Lambdas and HOF? Most “entrenched” OO developers don’t bother with them, that’s my experience.

I still believe that the barrier is up there.

1 Like

Here, you definitely have a point. Figuring how I was supposed to learn OCaml was… harder than it should have been. RWO is great, but it would be helpful to have a good “official tutorial” that is concise and covers the main language features. There are tutorials on the official website… but… we all know about those. The manual is not too bad, but it’s quite dense and technical.

2 Likes

I would not go that far. (For example, operator()() is prevalent in the C++ world.) OCaml does, however, have a very peculiar feature: curryfication. The difference between f x y + f x z and let g = f x in g y + g z can be subtle, if not error-prone, depending on the definition of f. Most OO languages make a syntactic difference between a partly applied function and a function returning a closure. OCaml does not, which can be quite confusing for beginners.

As for the original question, CamlLight was the first programming language I encountered during my computer science curriculum. Nowadays, most tools I contribute to happen to be written in OCaml, so it has become my main programming language, though I am still a C++ programmer at heart.

2 Likes

I ran away from 12 years of C++. Couldn’t stand the complexity, compile times, etc.
OCaml is for recreational programming + some tools at work.

1 Like

I haven’t quite learned OCaml yet, because I’m still learning OCaml :smile: .

Being more of a web developer, I got introduced to OCaml via Reason, but once I started learning more about the core language, I instantly got hooked.

OCaml strikes such a good balance between being expressive, yet delivering excellent performance. I love that it’s also a very minimal language, but powerful enough to really build whatever you want.

OCaml makes me happy.

8 Likes

I’ve been fooling around with Rust for a couple weeks (that all? sure seems longer …), and it kind of makes me laugh to hear about a high barrier to OCaml. Goodness what a complicated language. The form is visually similar to C … so what?

It’s very popular, is this right? I can understand the attraction - no one cares if I use it or not, but I’m still on it, there’s much to like in this glittering edifice. But it’s like a covert intro to ML - heavily used Option type, let binding, etc. - and when the “Rustacean” eventually finds his or her way to OCaml, won’t it have a refreshing elegance by comparison? Starkly minimal by comparison, free of magic operations, blissfully simple memory management.

8 Likes