DEV Community

Discussion on: Getting your feet wet with OCaml

Collapse
 
jvanbruegge profile image
Jan van Brügge

I learned ocaml in university, I've read the article and I still dont get why people prefer ocaml over Haskell.
Yes, having a strongly typed language with a good compiler is amazing, but Haskell is that too. But then there are the things that ocaml lacks. Typeclasses for example. They are far simpler than the modules of ocaml and do a much better job. It is just plain annoying that + does not work for floats. Why do i have to use +.? In Haskell it is defined as Num a => a -> a -> a. But ocaml just lacks a way to express that. Then it's functional, but still allows you to to side effects all over the place. This is not the security I expect from a functional language. If I have a function from a to b, I do not want that function to call my database or launch missiles. But then again ocaml lacks a way of expressing the idea of monadic IO. In the end you have modules that you can use to emulate part of it, but it still feels more like duck typing than a thought out system.
Another thing is type signatures. Why do I have to put them in a different file? I want to have them near the function. And no, putting them inline is not an alternative for more complex types.
Then why are tuples defined with a star and not like they are used in the code with a comma? Why double semicolon? Why a tick before type variables? Why does multithreading has to be so much more complicated than in Haskell?
All in all, I just dont see a compelling reason to use ocaml when you could use haskell instead. Feel free to correct me.

Collapse
 
bobbypriambodo profile image
Bobby Priambodo

Hi Jan, thank you for your opinion. It's great that Haskell works for you.

Your critiques are indeed valid. On the typeclasses story, there are works on modular implicits that aim to solve that (your exact concerns are stated on the introduction of the paper).

I see OCaml as a friendlier introduction to FP; if you're uncomfortable with diving deep to FP, there are escape hatches available to use imperative constructs. Indeed that sacrifices the security you get like with Haskell, but then again everything is a trade-off. Reason shows that you can even incorporate a JS-ish syntax for it to fix many of the warts, which I guess is interesting to some demography of developers.

Haskell is a great language, and I'm sure Haskell programs are fast, but the development experience for me is slower than OCaml. The compilation takes a long time, ghc-mod is slow to query types, Intero is better but still nowhere near Merlin in my opinion. Compilation in OCaml is fast, either you compile to native, bytecode, or JS (with BuckleScript or js_of_ocaml).

No straightforward way to annotate type signatures is also one of the bad sides, it prevents type-driven development like what you can do with Haskell. But with how fast Merlin is and how good the type inference is, it doesn't feel that much limiting for me.

But that said, I still think both Haskell and OCaml are on the top of my favorite languages list, so... :)