TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Introduction to OCaml

229 pointsby jxubalmost 7 years ago

14 comments

friscoalmost 7 years ago
OCaml is a great language. We looked really seriously at using it at my startup for some critical things a while ago, when the company was very young, because some of the team knew it and we were inheriting a small code base in it. Ultimately immaturity of tooling (eg ocamlp4 vs ppx) and near total lack of library support killed it for us. (We ultimately went with C++.) Good luck trying to use gRPC in it, for example: there are like three different &quot;protocol buffers&quot; libraries that all implement different parts of it and which are incompatible with each other. While Clojure and Scala have their weaknesses, being on the JVM and being able to re-use all of that Java infrastructure was such a massive advantage.<p>OCaml seems awesome and I wish I could use it more, but don&#x27;t underestimate how much time you&#x27;ll spend fighting with opam (and a lot of things just aren&#x27;t in opam at all, or the opam version isn&#x27;t compatible with your OS, but there&#x27;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.
评论 #17737465 未加载
评论 #17749203 未加载
georgewfraseralmost 7 years ago
The ML family languages are <i>still</i> so far ahead of &quot;mainstream&quot; languages in many ways. The first language I ever really learned was SML, my freshman year of college, and I swear there&#x27;s still a part of me that reaches for &quot;case&quot;, even after 15 years and about 1m lines of Java.<p>I&#x27;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&#x27;t have any object-oriented baggage. I think there&#x27;s real potential for a functional companion to Go, much like F#::C# and Scala::Java.
评论 #17737487 未加载
评论 #17737038 未加载
评论 #17736955 未加载
stcredzeroalmost 7 years ago
<i>Do not make it REPL-centric. Real life programs are not developed in the REPL. Include examples of complete programs.</i><p>In certain environments&#x2F;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 &quot;developed in the REPL.&quot;
评论 #17736400 未加载
评论 #17736778 未加载
评论 #17736288 未加载
评论 #17736318 未加载
评论 #17736531 未加载
评论 #17736787 未加载
评论 #17736505 未加载
评论 #17736256 未加载
kccqzyalmost 7 years ago
Do not forget the Real World OCaml book! I personally found it to be very well written.<p>Available both online and as a printed book published by O&#x27;Reilly: <a href="https:&#x2F;&#x2F;v1.realworldocaml.org&#x2F;v1&#x2F;en&#x2F;html&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;v1.realworldocaml.org&#x2F;v1&#x2F;en&#x2F;html&#x2F;index.html</a>
评论 #17736348 未加载
thereinalmost 7 years ago
My compilers course in college was taught in OCaml. The first semester of the course was spent on teaching functional programming and OCaml and the second semester was spent on implementing a compiler that compiled a restricted subset of OCaml.<p>It is a pleasure to write a tokenizer and a lexer in OCaml.
评论 #17736235 未加载
girzelalmost 7 years ago
I thought this was a pretty great introduction. Part 2, in particular, finally helped me understand function type signatures in ML languages: the fact that, under the hood, functions only accept a single argument, and the way the type signatures are presented are merely an aesthetic cleanup of what it actually looks like (which would include more parentheses to indicate associativity).<p>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:<p><pre><code> let this_integer = read_int () let _ = print_int (this_integer * this_integer); print_newline () </code></pre> But this doesn&#x27;t:<p><pre><code> let this_integer = read_int () in let _ = print_int (this_integer * this_integer); print_newline () </code></pre> And nothing in the tutorial explains why that should be so -- at least not that I could find.<p>I&#x27;ve also yet to see a really good explanation of when you need to use parentheses for grouping and when you don&#x27;t -- this is the only part of the language that really feels like a &quot;syntax error&quot; to me. Like, &quot;Look, ocaml is clean and doesn&#x27;t need to use all this extraneous syntax! Except when it does.&quot;<p>Anyway, good introduction.
评论 #17740393 未加载
评论 #17749195 未加载
merittalmost 7 years ago
In addition to books&#x2F;tutorials&#x2F;etc, I&#x27;ve found reading the source to unison [1] was a great way to see a more tangible example of using OCaml to create software<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;bcpierce00&#x2F;unison" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bcpierce00&#x2F;unison</a>
xamlhackeralmost 7 years ago
I loved OCaml as well but the lack of libraries pushed me to another ML variant: F# on .net platform. With .net core and F# tooling around it gradually maturing, it is a solid cross-platform functional language.
zumualmost 7 years ago
Awesome. I&#x27;ve been getting into OCaml lately, and it&#x27;s great to see new resources. This bodes well for future adoption.<p>For those looking for a slightly more comprehensive source, check out <a href="https:&#x2F;&#x2F;dev.realworldocaml.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;dev.realworldocaml.org&#x2F;</a> (no affiliation).
alkonautalmost 7 years ago
What are the biggest differences between OCaml and F#? If I wanted to learn&#x2F;use an ML style language 2018, for what purposes should I use or learn one over the other?
评论 #17740459 未加载
foobawalmost 7 years ago
OCaml was a required course in school for me. It was immensely useful and allowed me to think differently about programming (I only knew Java,C,C++ at the time).
gfioravalmost 7 years ago
Super late to the party but, if you love OCaml and ML languages, check out Elixir. Runs on the Earlang machine and this inherits all its libraries. It takes more than a page from patterns and conditional method bodies. Love it.
评论 #17741761 未加载
nv-vnalmost 7 years ago
For those considering learning OCaml, I thought I&#x27;d share my (admittedly biased) thoughts about the language as a long-time user (well... roughly 4.5 years).<p>Out of all the languages I&#x27;ve learned, OCaml is one of the few that I would consider to be a &quot;sweet spot&quot; language. A lot of people seem to have one language that they tend to fall back to when they&#x27;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&#x27;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&#x27;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.<p>There&#x27;s still a few things that are far from perfect, though. OCaml still lacks an equivalent to Haskell&#x27;s typeclasses and that makes designing good generic libraries a pain (it&#x27;s still possible using modules+functors, but it takes a little boilerplate because it&#x27;s not implicit). As a side effect of this, the standard library is pretty fragmented between the official one (aka &quot;things we used to write the compiler so you can keep them if you want&quot;), the Batteries library (which is essentially what the standard library would be if the official one was &quot;finished&quot;), and Jane Street&#x27;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&#x27;s little room for all the competition and that means that a lot of libraries either don&#x27;t exist or aren&#x27;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&#x27;t compatible with NPM, which means that BuckleScript (the OCaml-&gt;JS compiler) has a totally separate ecosystem from the native compiler.<p>Tl;dr, if you&#x27;re looking for a very general language to learn and don&#x27;t care about having to implement your own libraries, OCaml is one of the best choices out there. If you&#x27;re doing JS programming, it&#x27;s worth taking a look into ReasonML&#x2F;BuckleScript.<p>If you&#x27;re interested, feel free to ask me any questions about the language&#x2F;ecosystem&#x2F;learning.
评论 #17737645 未加载
kasajianalmost 7 years ago
where&#x27;s the PDF version?
评论 #17735943 未加载
评论 #17749164 未加载