Introduction to OCaml
blog.baturin.org
blog.baturin.org
OCaml seems awesome and I wish I could use it more, but don't underestimate how much time you'll spend fighting with opam (and a lot of things just aren't in opam at all, or the opam version isn't compatible with your OS, but there's an apt package for it, but oh no what is going on) and just trying to get to talk to the rest of your stack.
If you have more questions on F# i recommend the community forum http://forums.fsharp.org/
I am not an expert, yet, but F# is missing several of the more sophisticated OCaml features, but on the positive side, you have the .net core ecosystem
I've been playing around with an implementation of the ML semantics+type system, using the Go AST as a compilation target. Golang makes a surprisingly great compilation target for a functional language, because Go itself doesn't have any object-oriented baggage. I think there's real potential for a functional companion to Go, much like F#::C# and Scala::Java.
Sounds cool. Is it open source? I've attempted to write some ML interpreters and compilers, but I've always hit a brick while when I try to move from a basic "mini-ML" type of language -- which is relatively straight forward -- to one including pattern matching.
f# and ocaml aren’t even hard to learn! i mean, one can learn Standard ML in a couple of weeks using dan grossman’s programming languages course! i understand, f# and sml and even some scala after just some small reading. but i took a semester long course in c++ and never understood it.
Can you explain why you think so?
python is a very pedestrian language. that is probably the secret to its popularity. it makes people who have put no study into programming languages feel great because they're able to accomplish things quickly, at least at the start. however, they are missing a lot, in my opinion by sticking with python. there is nothing, from my perspective, that python has that f# doesn't or couldn't have. and the reverse of that is explicitly not true.
ml and lisp/scheme are two of the greatest programming language approaches and paradigms, and python chose and continues to choose to ignore both of them.
Specially since adding to what you mentioned, those languages were compiled since the early days.
It all boils down to lack of love from OS vendors.
Even nowadays, F# is still the black swan of .NET languages, with less tooling support that C++ gets.
So I came to appreciate the small additions we get in Java, C#, C++ to get closer to FP kind of programming than expecting a radical change.
https://www.amazon.com/OCaml-Very-Beginning-John-Whitington/...
https://www.amazon.com/More-OCaml-Algorithms-Methods-Diversi...
In certain environments/languages, real programs are developed in the REPL. Real programs, managing billion dollar portfolios, natural gas scheduling, and the payroll of Fortune 500 companies have essentially been "developed in the REPL."
In Common Lisp, Clojure, Scheme/Racket and Haskell, I constantly develop with the REPL. I also use it in other languages such as Ruby which are not necessarily REPL-centric.
I find that I write much better (less buggy) code, faster, in a language with great support for a REPL.
EDIT: http://pchristensen.com/blog/articles/are-you-this-agile-pau...
> This is a step beyond that. I'm not even doing releases; I've been pasting the changes into the running server's repl.
https://news.ycombinator.com/item?id=99586
Obviously it's a bit extreme to be hacking on the production site with no version control etc, but HN was a lot smaller back then.
Real World Haskell[1] is a great example of an introduction that provides small examples AND situates them in the context of programs that I could see myself actually using.
Because at the end of the day, to build your program, you need to have code in a source file of disk Code in the REPL is not persistent
So unless the language come with some sort of repl server, which persist code on a server, and the program instantiate themselves from the server (something i have never seen and i doubt that this is what you meant)
I am guessing you actually mean, that development was REPL supported, and that you used the repl to test your code, and help you design your code
And I see no problem in that, but still you need a solid traditional cycle around that to actually build, deploy and distribute your code ... right
Because at the end of the day, to build your program, you need to have code in a source file of disk
Not necessarily in the way you'd imagine. In Smalltalk, there is what amounts to a transaction log of your code changes, which you can replay. There is also an operation analogous to a database "checkpoint" where you save the whole memory image.
In most production environments, there is a 3rd mechanism which also tracks and saves the configuration in a database, which also serves as part of a build/deployment mechanism.
Code in the REPL is not persistent
In Smalltalk, all code executed in one of the REPL-like things is recorded on the transaction log of your code changes.
I am guessing you actually mean, that development was REPL supported, and that you used the repl to test your code, and help you design your code
In many Smalltalk environments, there are REPL-like things everywhere, including the many of the fields/panes of the debugger, and seasoned Smalltalkers would pretty much write the system in the debugger.
A typical MIT Lisp Machine OS in the 80s recorded for all functions/etc. where they were defined and one edits a function by calling (ed 'foo) (or by pressing M-. in the editor on a function name) and then the source file with the function gets edited. So the Lisp IDE knows that foo is defined in the file hans:graphics;editor;foo.lisp.273 (where 273 is the version number or the file). If one saved an image, this information is stored and restored on starting the image. Common Lisp systems today still record the source location, but they lack the versioned file system and they also usually not record a particular location from a source code version system like SVN or GIT.
What Xerox Smalltalk did (and something similar did Xerox Interlisp-D) was to manage source code in a distribution file and a changes file. Thus where the MIT Lisp Machine IDE tracks a multitude of versioned files (and the user has to somewhat manage them by creating them, registering them, compiling, loading them), the Xerox Smalltalk abstracts the editing away from files and manages the source storage in a file mostly invisible for the user. Thus a Smalltalk class browser does not display files, but it displays individual definitions. Since the compiled code is some high-level byte-code, source code can also in a more limited way computed from the compiled code.
Similar: Xerox Interlisp-D (which was a Lisp IDE, which ran on the same hardware as the Smalltalk machines from Xerox), which also managed source code (using tools like the File Package and Masterscope), but the source code is actually Lisp data and the main code editor is a structure editor - not a text editor. Xerox called it a residential system - since the source is actually data in the image and the File Package manages to track the changes and keep external versions of the source.
I have actually no idea which ML also a) works with images and b) manages source code. Would be interesting to hear more about that.
The article suggests that the repl is of use for finding the type of things. This is only half true. Consider the following:
type 'a my_extra_args = foo -> bar -> 'a
val some_fn : (int -> unit) my_extra_args
Now suppose you want to know the type of the function so you ask at the repl: utop> some_fn;;
val - : (int -> unit) my_extra_args
And now if you want to know how to call this function you need to try to guess arguments until a type error expands the type.Alternatively you could use compiled ocaml, write your programs in a sensible editor and then query the type using Merlin (a program to provide ide-like features to editors for ocaml)
On the other hand, I would expect a Common Lisp tutorial to talk about the repl as real world programs are written with the repl (but also in files)
Available both online and as a printed book published by O'Reilly: https://v1.realworldocaml.org/v1/en/html/index.html
It is a pleasure to write a tokenizer and a lexer in OCaml.
One of the examples did refuse to compile for me, and I felt Part 1 was still too light on basic syntax. I tried to do the exercise at the end of Part 1, about prompting for, squaring, and printing an integer. This works:
let this_integer = read_int ()
let _ = print_int (this_integer * this_integer); print_newline ()
But this doesn't: let this_integer = read_int () in
let _ = print_int (this_integer * this_integer); print_newline ()
And nothing in the tutorial explains why that should be so -- at least not that I could find.I've also yet to see a really good explanation of when you need to use parentheses for grouping and when you don't -- this is the only part of the language that really feels like a "syntax error" to me. Like, "Look, ocaml is clean and doesn't need to use all this extraneous syntax! Except when it does."
Anyway, good introduction.
That is a little weird, but not terrible. Thanks for the explanation.
let _ =
let this_integer = read_int () in
print_int (this_integer * this_integer); print_newline ()
I agree part 1 should explain more of the basic syntax. The whole thing is a work in progress so I keep improving it as I get feedback.For those looking for a slightly more comprehensive source, check out https://dev.realworldocaml.org/ (no affiliation).
I think if you have the choice not to use F#, though, that OCaml is generally nicer to use. It offers a ton of features that I find myself missing in F#, like a much more powerful module system, a more advanced approach to OOP, a cool macro system, and so many more (polymorphic variants, better exceptions/soon to be an effects system, ...). In general, I find the syntax to be slightly more predictable for OCaml, but a lot of people still prefer F#'s syntax (which is indentation-based, btw) so I wouldn't focus on that point.
Overall, I think they fit different niches. I've had to use F# quite a few times and it's by no means a bad language, but after using OCaml extensively I feel disappointed by F# a little bit like how I'd feel disappointed if I had to write a project in Java.
Out of all the languages I've learned, OCaml is one of the few that I would consider to be a "sweet spot" language. A lot of people seem to have one language that they tend to fall back to when they're not sure what else to use because they find it most practical, whether or not they enjoy using it as a language. Out of the languages I know, it's pretty much between Java and OCaml for me, with OCaml being much more ergonomic. Writing OCaml code is much more relaxing than most other languages I've encountered because everything is quite predictable once you know the core language, but it features a multitude of tools that you can use to approach any task (OOP, modules, functional programming, imperative, low-level, high-level, metaprogramming, etc.). I also think that OPAM is one of the best language package managers around (for example, it comes with native support for having multiple copies of the OCaml toolchain installed in parallel). Finally, Reason+BuckleScript have become really nice and for web programming I think they offer one of the best options.
There's still a few things that are far from perfect, though. OCaml still lacks an equivalent to Haskell's typeclasses and that makes designing good generic libraries a pain (it's still possible using modules+functors, but it takes a little boilerplate because it's not implicit). As a side effect of this, the standard library is pretty fragmented between the official one (aka "things we used to write the compiler so you can keep them if you want"), the Batteries library (which is essentially what the standard library would be if the official one was "finished"), and Jane Street's Core library (which replaces the standard library altogether). The problem is that this extends into basically all of OCaml. For such a small language, there's little room for all the competition and that means that a lot of libraries either don't exist or aren't actively maintained. That said, most libraries are a breeze to implement and for the real-world code that I do write, they are hardly a distraction 99% of the time. The only other downside is that OPAM isn't compatible with NPM, which means that BuckleScript (the OCaml->JS compiler) has a totally separate ecosystem from the native compiler.
Tl;dr, if you're looking for a very general language to learn and don't care about having to implement your own libraries, OCaml is one of the best choices out there. If you're doing JS programming, it's worth taking a look into ReasonML/BuckleScript.
If you're interested, feel free to ask me any questions about the language/ecosystem/learning.
What is your general prognosis for the future?
Are you by chance getting paid to write OCaml? If so, how hard was it to find opportunities.
2. I think it's kind of a bittersweet future. I think growth is decelerating for OCaml because of ReasonML/BuckleScript making it really easy to get started with OCaml and use web libraries. This is great because it brings exposure to the language, but I think it's caused a number of people to switch over from native to web, which means abandoning some of the OCaml libraries for their web versions. OCaml has a great native backend and it's really a shame for me to see it fade away, but I think in the long-run that's probably going to happen. Still, I think popularity of Reason will continue to go up and that will bring OCaml into the mainstream for hopefully long enough to garner some attention and get people to take the native backend seriously.
3. I'm currently working for equity at a very small startup I co-founded but we're using ReasonML for the web side of things. Previously, I was at an internship where I had a lot of freedom to use whatever I wanted and was paid to write some software in OCaml. I think finding a job writing OCaml is rather difficult, but introducing it in a job (especially for internal tools or web development with Reason) is much, much easier. The "big" OCaml jobs seem to all be at companies where it's really hard to find employment (Jane Street, Facebook, Bloomberg), but occasionally you'll see some smaller ones pop up online.
I'm going to keep hacking away at OCaml, but measure my expectations a little and perhaps start looking into Reason as well.
A couple questions wrt Reason:
Do you find it easy to switch between OCaml / Reason? I personally find the syntax of Reason a little unnecessary.
Do you use ReactReason or some other framework -- perhaps the Elm-like 'tea' package?