4 ms·
The big distinguishing difference is that OCaml doesn't have ad-hoc polymorphism and function overloading. You cannot have a function that operates on many diff
by atombender 7y ago
The big distinguishing difference is that OCaml doesn't have ad-hoc polymorphism and function overloading. You cannot have a function that operates on many different types; which is why you have two sets of arithmetic operators for integers and floats ("+" vs "+.", for example). In theory you can use modules for this, but it's awkward, and I don't think they're generally used this way. (OCaml is getting implicit modules, and this may help the situation.)
In Haskell, typeclasses make this super easy. It allows you define a family of functions/operators that together become what a Go or Java developer might call an interface. Code written to use a particular typeclass will work for any implementation of that typeclass.
On the other hand, OCaml's module system is very powerful and has no counterpart in Haskell. It allows you to package a whole type implementation into a single module that has a public signature, and can be instantiated with generic type parameters.
- chongli 7y agoI don’t know anything about OCaml so please forgive me if this question seems silly. two sets of arithmetic operators for integers and floats What if I want to create more numeric types? Say vectors in R^n, for example. Do I need to create more operators for vector addition and scalar multiplication? How do I make sure the user can’t add vectors of incompatible dimension?
- atombender 7y agoYes, you need to add new functions. You cannot redefine "+" to work a custom vector type. Similarly, you cannot have a "print" function that works for all sorts of types. OCaml has print_string, print_int, and so on. OCaml does have parametric polymorphism ("generics"). You can write functions which support generic parameters. And you can use modules as "functors" to sort of package up types in a similar way as Haskell typeclasses (e.g. see [1]). [1] https://stackoverflow.com/questions/14934242/whats-the-closest-thing-to-haskells-typeclasses-in-ocaml https://stackoverflow.com/questions/14934242/whats-the-close...
- vmchale 7y ago> OCaml's module system is very powerful and has no counterpart in Haskell. Is this taking into account backpack?
- atombender 7y agoI'm not familiar with Backpack, but reading about it [1], it looks like its goal is indeed to provide an OCaml-style module system to Haskell. [1] https://plv.mpi-sws.org/backpack/ https://plv.mpi-sws.org/backpack/