frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

OCaml Programming: Correct and Efficient and Beautiful

https://cs3110.github.io/textbook/cover.html
108•smartmic•9h ago

Comments

teiferer•8h ago
Could an OCaml expert give a quick take on the view that if FP, why not go all the way and do Haskell instead? I mean, if "correct, efficient, beautiful" are attributes of OCaml (and I know opinions differ, but let's assume for a moment..) then shouldn't they be attributes of Haskell too, maybe even more so in some ways?
skybrian•7h ago
I'm not an OCaml or Haskell expert, but I expect that laziness makes performance harder to reason about?

Edit: but in this case, apparently the book was written for a course at Cornell where they teach both functional and imperative programming using the same language.

derriz•7h ago
In my experience, reasoning about (or maybe being able to manage) memory consumption is more of an issue for fully lazy languages.

Having worked on a code-base written in a lazy (Haskel-like) FPL where the architecture relied heavily on laziness, the least pleasant issue to deal with was sudden and obscure explosions in memory consumption.

Which would of course have a big impact on performance so maybe this was your point also.

hermanhermitage•7h ago
That’s been my experience. Across the spectrum of languages I’ve found certain features such as dynamic memory, managed memory, memory safety, evaluation model, etc all have an impact on the transparency of understanding time and space characteristics. I spend most of my time in assembly and C, but love the sheer ranges of language options we have today. I put Haskell in the “Miranda” branch of languages which I love for tackling some problems but day to day but I’ve never got a handle on how that translates into predictable characteristics of the generated code.
OneDeuxTriSeiGo•7h ago
At least theoretically they could be however OCaml is in large part driven by Jane Street and has been for some time now and Jane Street's entire business model is built around optimizing for ultra high throughput, ultra low latency software where mistakes could cost on the order of hundreds of billions of dollars.

So my guess would be less that Haskell is not these things (nor couldn't it be) but rather that OCaml has had the external forces necessary to optimise for these things above all else.

AnimalMuppet•7h ago
Is OCaml lazy? I'm not an expert, but if you want ultra high throughput, you might not want lazy. If I understand correctly, in Haskell some nonobvious things can slow you down because of the laziness.
sestep•7h ago
No, OCaml is not lazy.
KiranRao0•7h ago
OCaml is not lazy.

src: https://signalsandthreads.com/future-of-programming/

shawn_w•7h ago
Personally, I don't like how Haskell considers things like I/O as side effects that have be wrapped in monads. Ocaml feels much more practical.

Plus, though both languages allow defining new infix operators, ocaml coders are much more restrained about it than haskellers, and I hate having to figure out what >>>>$=<<< does when it shows up in code.

Quekid5•6h ago
*. sends its regards

:)

faldor20•7h ago
As someone who has lightly used Haskell and quite heavily used ocaml:

- Pragmatism: The ocaml community has a stronger focus on practical real projects. This is shown by the kind of packages available on the ecosystem and the way those packages are presented. (A number of Haskell packages I've tried to use often seen to be primarily intellectual pursuit with little documentation on actually using them).

- Simplicity: Haskell does have some amazing features, but it has so many different ways to do things, so many compiler flags, code can look vastly different from one codebase to another. It's kind of the c++ of FP. Ocaml is definitely more cohesive in its approach.

Tooling: last I used Haskell the whole ecosystem was pretty rough, with two package managers and a lot of complexity in just setting up a project. Ocaml is not at the level of rust or other modern languages, but is definitely a stop above I'd say.

Syzygies•7h ago
I started with SML in the 1980's, implementing a core math algorithm (Grobner bases) used in my K&R C computer algebra system Macaulay. Then I got this idea there should be a related algorithm in a different problem domain (Hilbert bases) and I managed to convert my code in twenty minutes. It ran. This completely blew my mind, on par with switching from punched card Fortran to an APL terminal in the 1970's.

Everyone talks a good line about more powerful, expressive programming languages till they need to put in the work. Ten years effort to program like ten people the rest of your life? I'm 69 now, I can see how such an investment pays off.

I moved to OCaml. Studying their library source code is the best intro ever to functional programming, but I felt your "all the way" pull and switched to Haskell. (Monads make explicit what a C program is doing anyway. Explicit means the compiler can better help. What it comes down to is making programming feel like thinking about algebra. This is only an advantage if one is receptive to the experience.)

I'm about to commit to Lean 4 as "going all the way". Early in AI pair programming, I tested a dozen languages including these with a challenging parallel test project, and concluded that AI couldn't handle Lean 4. It keeps trying to write proofs, despite Lean's excellence as a general purpose programming language, better than Haskell. That would be like asking for help with Ruby, and AI assuming you want to write a web server.

I now pay $200 a month for Anthropic Max access to Claude Code Opus 4 (regularly hitting limits) having committed to Swift (C meets Ruby, again not just for macOS apps, same category error) so I could have first class access to macOS graphics for my 3-manifold topology research. Alas, you can only build so high a building with stone, I need the abstraction leverage of best-in-category functional languages.

It turns out that Opus 4 can program in Lean 4, which I find more beautiful than any of the dozens of languages I've tried over a lifetime. Even Scheme with parentheses removal done right, and with far more powerful abstractions.

pmarreck•5h ago
Have you looked at Idris 2?

I'm 53, impressed that you're still going at it at 69!

Syzygies•3h ago
Yes. I'm impressed with Idris 2. I love how it uses Chez Scheme, my favorite scheme implementation. I contributed for a bit to getting Idris installation working on Apple Silicon Macs based on Racket's port of Chez Scheme, only to learn that I was working with Idris instructions that hadn't been updated.

Lean 4 is a better supported effort, with traction among mathematicians because of the math formalization goal.

I have more reasons to want to learn Lean 4. Peel away their syntax, and Lean 4 proofs are the word problem for typed trees with recursion. I find the reliance of AI on artificial neurons as arbitrary as so many advanced life forms on Earth sharing the same paltry code base for eyes, a nose, a mouth, and GI tracts. Just as many physicists see life as inevitable, in a billion runs of our simulation I'm sure AI would arise based on many foundations. Our AI fakes recursion effectively using many layers, but staring at the elementary particles that make up Lean proofs one sees a reification of thought itself, with recursion a native feature. I have to believe this would make a stronger foundation for AI.

I don't get that same rush looking at Idris. Using Lean 4 for general purpose programming? It must be good training.

pmarreck•1h ago
I'll have to have a look at Lean 4 then.

The simulation hypothesis has a flaw IMHO- If it is modelable and therefore computable, it may be subject to the halting problem

yodsanklai•6h ago
Pros and cons as usual. I worked with both languages professionally but I personally find OCaml more practical, better at programming in the large and easier to write maintainable code. It's a simpler language overall (side effects, strict evaluation). I find the language finds sweet spot between these attributes whereas Haskell is more abstract.

That being said, Haskell is pretty nice as well but I'd pick OCaml for real world stuff.

One thing that bothered me with both these languages is that people not fluent with FP could write code that isn't idiomatic at all. It's probably a bit harder to do in Haskell.

mbac32768•6h ago
FP is great but not necessarily at all costs.

OCaml is immediate by default instead of lazy, and allows imperative code with side-effects. Both escape hatches from the pure FP world.

So, performance is easier to reason about and you can interact with your side-effecty real world stuff without having to reorganize your whole program around the correct monad.

Most of the time you want your loops to be higher order functions but once in awhile you want to just build a vector from a for loop. OCaml let's you do it without it being a whole intervention.

droideqa•5h ago
What about Haskell STM versus OCaml Multicore Eio?
kcsrk•5h ago
Kcas is the Haskell STM analogue in OCaml https://github.com/ocaml-multicore/kcas/
nextos•5h ago
Also, a major differentiator is that Haskell deals with effects primarily by using monads, whereas many modern functional languages (e.g. Koka or Flix) are being designed from the ground up to use algebraic effects instead. OCaml is also embracing effects. Haskell has some effect libraries as well, but monadic code is everywhere. IMHO, as someone who loves Haskell, algebraic effects will make FP much more approachable.
KevinMS•5h ago
My take is that OCaml lets you sneak a little mutation in, with a little effort, which can make a huge difference in the performance of some algorithms.
iLoveOncall•8h ago
There's absolutely nothing efficient nor beautiful about programming in OCaml.
yawaramin•6h ago
Username checks out.
crvdgc•7h ago
Apart from being tied with Jane Street's libraries, Real World OCaml is "deeper" in that it also talks about implementation details. This book has a more "introduction to functional programming via OCaml" vibe. Both are good textbooks with different emphases.

The main author Michael Clarkson also started a similar lecture series on Software Foundations using Rocq (Coq)[1]. Not sure if that's still updated though.

[1]: https://github.com/clarksmr/sf-lectures

pizlonator•4h ago
I would take the FP zealots more seriously if they stopped asserting that FP makes things more correct.

Zero evidence that this is the case.

I can tell you that debugging a compiler written in ML is a dumpster fire compared to debugging a compiler written in C++. If take C++ over any FP language for compilers any day of the week.

yawaramin•2h ago
OCaml is not just FP. It's FP + strong static typing + modular programming + exhaustive pattern matching + fast compiles + great set of built-in compiler lints (eg unused code warnings, mutation warnings). All of these things together help write very reliable code.
pizlonator•1h ago
There is no evidence that any of those things lead to more correctness.

It’s all feels

yawaramin•1h ago
There's plenty of evidence. Here's the OCaml compiler catching a redundant rule in the Unicode line-breaking algorithm: https://www.unicode.org/mail-arch/unicode-ml/y2020-m03/0000....

People who like rejecting this kind of stuff as 'feels' are ironically also being guided by their 'feels'.

pjmlp•1h ago
Yet all mainstream languages, including C++, keep adding FP concepts.

Better spend some time having fun with std::variant, visit, and ranges transformers.

anon-3988•3h ago
I just want OCAML to have curly braces, please. And the variable scope thing in OCAML is extremely off putting.
debugnik•3h ago
You can use the ReasonML syntax with the standard OCaml toolchain, it's the same language with curly braces. (Not to be confused with ReScript which spun off of it, but is now a different language that only targets the JavaScript stack.)

Do you simply dislike the OCaml syntax or is it some particular quirk?

> the variable scope thing

The what thing? Variables are just lexically scoped, are you referring to shadowing?

zdragnar•3h ago
Have you tried reason? Seems like a step in the direction you're looking for anyway

Chemical process produces critical battery metals with no waste

https://spectrum.ieee.org/nmc-battery-aspiring-materials
39•stubish•2h ago•3 comments

Fast and cheap bulk storage: using LVM to cache HDDs on SSDs

https://quantum5.ca/2025/05/11/fast-cheap-bulk-storage-using-lvm-to-cache-hdds-on-ssds/
47•todsacerdoti•3h ago•11 comments

Smallest particulate matter sensor revolutionizes air quality measurement

https://www.bosch-sensortec.com/news/worlds-smallest-particulate-matter-sensor-bmv080.html
26•Liftyee•3h ago•3 comments

A low power 1U Raspberry Pi cluster server for inexpensive colocation

https://github.com/pawl/raspberry-pi-1u-server
16•LorenDB•3d ago•6 comments

Resizable structs in Zig

https://tristanpemble.com/resizable-structs-in-zig/
108•rvrb•9h ago•48 comments

Show HN: QuickTunes: Apple Music player for Mac with iPod vibes

https://furnacecreek.org/quicktunes/
70•albertru90•7h ago•18 comments

How we rooted Copilot

https://research.eye.security/how-we-rooted-copilot/
289•uponasmile•14h ago•110 comments

Purple Earth hypothesis

https://en.wikipedia.org/wiki/Purple_Earth_hypothesis
211•colinprince•3d ago•58 comments

Low cost mmWave 60GHz radar sensor for advanced sensing

https://www.infineon.com/part/BGT60TR13C
56•teleforce•3d ago•22 comments

Personal aviation is about to get interesting (2023)

https://www.elidourado.com/p/personal-aviation
81•JumpCrisscross•7h ago•80 comments

16colo.rs: ANSI/ASCII art archive

https://16colo.rs/
20•debo_•3d ago•4 comments

Rust running on every GPU

https://rust-gpu.github.io/blog/2025/07/25/rust-on-every-gpu/
520•littlestymaar•20h ago•176 comments

Paul Dirac and the religion of mathematical beauty (2011) [video]

https://www.youtube.com/watch?v=jPwo1XsKKXg
55•magnifique•8h ago•4 comments

Coronary artery calcium testing can reveal plaque in arteries, but is underused

https://www.nytimes.com/2025/07/26/health/coronary-artery-calcium-heart.html
65•brandonb•8h ago•57 comments

What went wrong for Yahoo

https://dfarq.homeip.net/what-went-wrong-for-yahoo/
155•giuliomagnifico•12h ago•146 comments

Getting decent error reports in Bash when you're using 'set -e'

https://utcc.utoronto.ca/~cks/space/blog/programming/BashGoodSetEReports
100•zdw•3d ago•30 comments

The natural diamond industry is getting rocked. Thank the lab-grown variety

https://www.cbc.ca/news/business/lab-grown-diamonds-1.7592336
182•geox•18h ago•221 comments

Teach Yourself Programming in Ten Years (1998)

https://norvig.com/21-days.html
70•smartmic•9h ago•24 comments

Torqued Accelerator Using Radiation from the Sun (Tars) for Interstellar Payload

https://arxiv.org/abs/2507.17615
47•virgildotcodes•8h ago•4 comments

Tinyio: A tiny (~200 line) event loop for Python

https://github.com/patrick-kidger/tinyio
66•tehnub•4d ago•14 comments

Where are vacation homes located in the US?

https://www.construction-physics.com/p/where-are-vacation-homes-located
86•rufus_foreman•12h ago•69 comments

Arvo Pärt at 90

https://www.theguardian.com/music/2025/jul/24/the-god-of-small-things-celebrating-arvo-part-at-90
65•merrier•9h ago•19 comments

Shallow water is dangerous too

https://www.jefftk.com/p/shallow-water-is-dangerous-too
108•surprisetalk•3d ago•63 comments

Janet: Lightweight, Expressive, Modern Lisp

https://janet-lang.org
17•veqq•5h ago•1 comments

Test Results for AMD Zen 5

https://www.agner.org/forum/viewtopic.php?t=287&start=10
220•matt_d•11h ago•42 comments

Large ancient Hawaiian petroglyphs uncovered by waves on Oahu

https://www.sfgate.com/hawaii/article/hawaii-petroglyphs-uncovered-20780579.php
79•c420•4d ago•23 comments

Asyncio: A library with too many sharp corners

https://sailor.li/asyncio
84•chubot•7h ago•67 comments

Millet mystery: A staple crop failed to take root in ancient Japanese kitchens

https://phys.org/news/2025-07-millet-mystery-staple-crop-root.html
25•PaulHoule•3d ago•9 comments

Open Sauce is a confoundingly brilliant Bay Area event

https://www.jeffgeerling.com/blog/2025/open-sauce-confoundingly-brilliant-bay-area-event
328•rbanffy•3d ago•186 comments

Font-size-adjust Is Useful

https://matklad.github.io/2025/07/16/font-size-adjust.html
163•Bogdanp•3d ago•51 comments