OCaml 4.03.0 released (including flambda)
sympa.inria.fr
sympa.inria.fr
Parametric polymorphism is when you can define a function without knowing the precise type of its parameters. However, this limits what you can actually do with the parameters.
For instance, a function concat([a], [a]) -> [a] which takes two lists containing items of type "a" knows enough about the type of its parameters (they're lists) to do its job, but it doesn't know everything about them. This is the type of polymorphism OCaml supports. Think of it as "one implementation, many types". That's why OCaml has more than one print function, and why it has integer and floating point versions of operators like + and *.
Ad hoc polymorphism is when you can define implementations for a single function for whatever types you like. So you can implement add(Int, Int) -> Int, as well as add(Float, Float) -> Float. So this is "many implementations, many types." Haskell supports a version of this through typeclasses. OCaml doesn't currently support it, but the new "modular implicits" feature, as I understand it, will provide largely the same functionality.
Haskell: concat :: [a] -> [a] -> [a]
OCaml: val concat : 'a list -> 'a list -> 'a list = <fun>
Both are parametrically polymorphic. However, in Haskell, I can use a type class to support ad-hoc polymorphism:
concat :: (Num a) => [a] -> [a] -> [a]
Now concat works forall a, as long as a is an instance of Num. This is important for arithmetic operators:
(+) :: (Num a) => a -> a -> a
Now I can have (+) be defined in a meaningful way, since Int, Integer, Float, etc. are all instances of Num, and implement (+). An easy way to think of this (since it's all it is, really) is operator/method overloading. In Haskell, (+) is overloaded for every type that you would want it to work for (the details of this are actually probably different, but are not important for learning).
In OCaml, if I have
val (+) : 'a -> 'a -> 'a = <fun>
There is actually only two implementations of that function:
let (+) a b = a
or
let (+) a b = b
Since I cannot restrict the set of types to the function, I can't do anything with the arguments. I don't know what the types are! Ad-hoc polymorphism allows for restricting the set of types, which allows for Haskell to use + for any Num-like thing.
EDIT: I think the line that stood out to me as being odd was "That's why OCaml has more than one print function, and why it has integer and floating point versions of operators like + and *." which was after you explained what parametric polymorphism was. So my interpretation was "OCaml supports parametric polymorphism, which is why there are multiple addition functions for different types." Of course, the real reason isn't due to supporting parametric polymorphism, but specifically because OCaml lacks support for ad-hoc polymorphism.
Interestingly, OCaml does have sort of pseudo-ad-hoc polymorphic implementations for comparison operators.
val (<) : 'a -> 'a -> bool = <fun>
But this is a wonderfully hacky implementation.
Anyway, the reason, along with having separate sets of arithmetic operators, is basically because Ocaml ‘generics’ can't be specialized like C++ templates can. Doing it this way keeps the whole language fully type-inferable.
BTW, I think https://github.com/c-cube/sequence is more in closer to the generic iterable datatype you are looking for. You still need to convert to and from the seq type but its pretty close...
That said, Ocaml does have some voodoo magic for type-safe printf, so in the specific case of printing you can use a single function if you want
let name = "Buttons840" in
Printf.printf "Hello, %s!\n" name- OCamlbuild is the bee's knees. Configuring complex projects requires just one line in the Makefile
- OPAM works wonderfully. No configuration required. Everything just works.
- Merlin (https://github.com/the-lambda-church/merlin) is incredible. If you use Vim / Emacs, you owe it to yourself to set it up and have better syntax highlighting, indentation and type information right within your editor.
- Approachable online guides to get started. RWO[1] is a fantastic resource and teaches you the good parts right from the start.
- Utop (https://github.com/diml/utop) is a very modern, fully-featured REPL (almost as good as IPython)
----------
If you've been thinking of learning OCaml, here are a few useful resources -
- Real World OCaml [RWO] - https://realworldocaml.org/
- Using, Understanding, and Unraveling the OCaml Language - http://caml.inria.fr/pub/docs/u3-ocaml/index.html
- Developing applications with OCaml - http://caml.inria.fr/pub/docs/oreilly-book/html/index.html
- System programming in OCaml - https://ocaml.github.io/ocamlunix/ocamlunix.html
Lastly, if you are out of ideas on what to build with OCaml, try out these courses for some challenging programming assignments
- Functional Programming (Princeton) : http://www.cs.princeton.edu/~dpw/courses/cos326-12/info.php
- Programming Languages and Compilers (UIUC): https://courses.engr.illinois.edu/cs421/fa2014/
EDIT: Sorry, realized I replied to the wrong comment. Meant to reply to the grandparent.
Then it gives you the error, and gives you a command to roll back everything to the previous state -- something I cannot do with eg, apt, as far as I know. But that's a bit apples and oranges -- apt, yum, pacman, etc are all binary package managers, with a different set of tradeoffs.
Linux distro package managers are usually decent, but most of them are binary package managers and don't offer the same set of feature, so it's a bit different (and they are a fair bit older too).
For some reason, OPAM defaults to installing dependencies globally and does not provide an easy way to support different workflows
Is tackling real world projects also an option?
Or is serving a web site something that is going to make you so proud of having done it with OCaml that you'll submit it to HN?
(This reply might seem saracastic but it is actually meant seriously - as in, does it make sense to actually use it in production?)
Long answer: as with anything, it depends. Any programming language is well-suited for some programming tasks and awkward for others. Except Lisp.
Well ... with 50 shades of gray in between. You can do pretty much anything with Python f.x. But can you write a web service in Haskell without having to hire a team of computer science PhDs? Not so sure ... but luckily you're nephew will happily realize it with PHP during holidays for a couple of bucks :E
The second number is also kinda major features, but not as large. Kinda similar to the Python release cycle. New features get introduced here, so things like multicore, flambda, effects, modular implicits would land here. It has a rather long release cycle but I heard there are plans to shorten the release cycle to something like roughly 6 months. These are 2 digits zero-padded for no real reason except maybe easier string sorting, so 4.03 would translate to 4.3 and 3.10 would be 3.10 and not 3.1.
Then the last digit is for patches and regression fixes that do not introduce new features.
Want purity, elegance and best community resources? Go with Haskell. Want Functional+OOP+kitchensink? Go with Scala. Want .NET? Go with F#. Want a strict/eager Haskell? Apply for a job at Standard Chartered (they say they have an in-house strict and better optimizing Haskell compiler).
What would X be, so that you would be able to say "Want X? Go with OCaml"?
When it comes to resources, I don't think Haskell is strictly superior. opam, merlin, and utop are pretty nice... I'm also very fond of the menhir parser generator.
On a theoretical level, Ocaml does have some neat things missing from Haskell, like its module system (with functors), polymorphic variants, etc...
---
BTW, I wouldn't dump Ocaml in the "object oriented" side of things together with Scala. Nowadays the OO infrastructure is more commonly used for polymorphic variants than for OO, to be honest.
In fact, I love and use both Haskell and Ocaml, and I view the two languages as cousins. Both are functional. Both have features the other doesn't. Ocaml has the best module system in the business, with functors for module abstraction (unbelievably useful). This is much better than the current Haskell module system, though Backpack may narrow the gap. Ocaml also has polymorphic variants, which are surprisingly useful. Ocaml has some convenience features like named/optional arguments that Haskell doesn't. And oddly enough, the lack of purity in Ocaml (if used judiciously) can be a real win. You can do imperative programming without jumping through a lot of hoops. You can have code which is purely functional from the outside but which uses imperative idioms internally for efficiency (yes, I know about unsafePerformIO in Haskell, but it's much easier to do this sort of thing in Ocaml). And Ocaml is usually faster both in compilation time and run time.
My overall take on it is that I prefer Haskell for small-scale programming but I prefer Ocaml for large-scale programming. YMMV.