Learning ocaml through a f# book?


#1

Hello, i was wondering if it was a good idea to try to learn ocaml through this Book, i heard that f# is pretty similar to ocaml, any thoughts on it ?


#2

I don’t claim to know F#, but no one’s answered yet, and … before I decided to learn OCaml, I looked at F# as well. I would worry that trying to learn OCaml from an F# book would be frustrating.

My understanding is that F# has an indentation-based syntax as well as a more OCaml-like syntax. Also, an F# book would be likely to incorporate .Net library calls into examples. In addition to these libraries being unavailable in OCaml, my guess is that they’d use an objected-oriented style syntax which is uncommon in OCaml. I’m sure there are lots of other little differences between F# and OCaml. F# started from OCaml, but the sense I have is that over time its developers have felt free to add and change things in whatever ways seemed best to them.

So to the extent that parts of a book focus on syntax and concepts that are more or less shared between OCaml and F#, you might learn OCaml with an F# book, but there’s no reason to assume that an author would try to focus on what’s common to the languages.

If you are a moderately experienced programmer, and especially if you have some experience with functional programming, I’d recommend Real World OCaml. Some things are out of date in the published version, but not much. I learned OCaml from it not too long ago, and not much has changed since then. If you like online reading you can read it for free. I like print for learning things like computer languages, and it’s a good thing to financially support the care that the authors and publishers put into the book if possible. (Publishers and editors don’t get enough credit for what they put into creating good books.) There is also a new version of Real World OCaml in the works that’s available online, but I’m not sure how much or what parts have been updated so far.

If you’re closer to being a beginning programmer or don’t feel at all comfortable with functional programming, OCaml from the Very Beginning is supposed to be good.

If you are stuck on a desert island with the F# book and a computer and an OCaml compiler, you could work through it, but otherwise, I suspect that using an F# book for OCaml will be uncomfortable. I don’t know the book you linked to, though, so who knows?


#3

Thank you for your reply, it was insightful and helpful (i actually didn’t thought about the difference of ecosystem between them and the fact that since it’s .Net it would be more OOP style than fp ).
I own Ocaml From The Very Beginning, More Ocaml and Initiation à La Programmation Fonctionnelle En Ocaml (a french book), so i guess i’ll use them alongside the new version of RWO (i should be fine since i have some fp knowledge through scala.)
Excited to get going and learn some ocaml !


#4

You don’t need a F# book.
Just read the manual:
https://caml.inria.fr/pub/docs/manual-ocaml/index.html
Especially chapters:
Chapter 1 The core language
Chapter 2 The module system
Chapter 4 Labels and variants
Chapter 5 Polymorphism and its limitations
Chapter 6 Advanced examples with classes and modules


#5

There are plenty of online resources on OCaml (Real world Ocaml, Think Ocaml) why not use them.
For any one else looking for an answer to this same question well I am not an F# expert not even and intermediate developer but I have been lookin into it and F# has some syntactical differences that would not get you by in OCaml world. for example

let mutable name = "Richard";;

The above code will never work in OCaml because variables can not be made mutable. However it works in F# Compiler.
Not to mention the difference of libraries and functions like printfn vs Printf.printf. F# uses printfn extensively in teaching where in OCaml you would use print_int, print_string or print_float for respective datatypes


#6

I came from F# to OCaml. There’s definitely a lot of overlap, and learning one is very helpful for learning the other later on, but I wouldn’t recommend trying to learn OCaml from F# material. There are just too many differences, some of which have been mentioned above:

  • Syntax is (by default) indentation-based in F#, which has no equivalent in OCaml
  • Modules are much less powerful and flexible in F#, mostly because (IIRC) they’re effectively special types of .NET class (you’d probably use objects a lot more often in F#, but personally I’ve hardly ever resorted to them)
  • F#'s object-oriented support is very different, being based on the .NET model, and arguably more prominent/more ‘useful’ than OCaml’s
  • There are lots of features OCaml has that F# doesn’t (polymorphic variants, functors, PPX, straightforward native compilation, to name a few)
  • There are lots of features F# has that OCaml doesn’t (.NET interoperability, type providers, units of measure, computation expressions, LINQ I think?)
  • Generally, the ecosystem is completely different (OCaml has dune and OPAM, F# has msbuild/dotnet and nuget)
  • Generally, the philosophy/use case is different. While both of them are quite pragmatic functional languages, and can be marketed as ‘secret weapons’ that enterprises can use for reducing complexity and improving the ability to reason about programs, F# is IMO more about pragmatically (and often gradually/incrementally) porting the benefits of functional programming to the existing OOP-dominated .NET ecosystem (with interoperability and compatibility being important), whereas OCaml is its own separate entity that can develop and expand as desired.

#7

fwiw a little bit of units of measure functionality might be provided in OCaml by Owl’s metric system constants.