Learn Programming with OCaml(usr.lmf.cnrs.fr) |
Learn Programming with OCaml(usr.lmf.cnrs.fr) |
I'm an old man, I learned the Standard ML of New Jersey when I was an undergraduate many years ago, but I'm pretty sure that experience though it isn't irrelevant, did not overly colour my belief in what we [speaking broadly of humanity] ought to teach.
It’s still an abstraction, but at least being aware of memory management, copying vs referencing, etc are hugely important concepts that ML languages can hide.
C is in an odd position right now to argue it is how the machine is really working. Computers are more complicated since bigger caches entered the picture. Hell I do not think even ASM is a good approximation on how machine really work given the data dependencies will make stuff being processed in parallel instead of sequentially.
What you could argue is that C is the archetype for an imperative language procedural language with a clean mapping to ASM. That is different than how the machine works. Simpler architecture have less distance between their ASM and what is really hapenning.
C is still heavily abstracted. Modern OS evolved in conjunction with C s.t. it behaves like a C runtime simulating a PDP11 (I remember reading a nice article on that). To learn system, what you need is an OS course, not C. I'd say the current C-based stack is in a quite embarrassing state.
Well some procedural language would do the trick.
The main issue is see is people jumping straight into OO or functional programming and developing a habit of over-abstracting everything.
I think something like Odin which is purely procedural but has less historical cruft and offers modern affordances would be a great choice.
C is absolutely worth learning but students will spent a lot of time learning to cope with its sharp corners and subpar standard library. Some might give up before they discover the joys for programming.
The hardware machine model used by the C programming language is completely obsolete and very different from how modern CPUs work. Even for abstracting PDP-11 it had defects and omissions.
The fact that C is indeed more transparent than many other more abstract programming languages does not make it good enough for understanding how the CPU runs your program. Believing that the CPU works within the straitjacket of the C language is dangerous, because much more than half of the instructions of a modern CPU cannot be directly expressed in C (though a good optimizing compiler can sometimes infer when such instructions can be used), and the C language does not even have the data types that are used by many hardware instructions.
The relationship between the C language and how a computer runs a program is exactly the opposite of what the previous comment says.
For someone who knows how a CPU runs a program, it will be easier to understand how a C program is run than how a program in another language is run, because for the latter there may be very different runtime library implementations, whose behavior cannot be guessed by looking at a source program, without having supplementary documentation about the compiler or interpreter that is used.
On the other hand if someone knows only the C programming language, their mental image about how the program is executed is likely to be very wrong for any non-trivial program.
For learning an assembly language, the prior knowledge of C is more a handicap than something helpful, by creating bad habits, like using incorrect implicit conversions, inappropriate integer data types, not caring about interactions with the cache memory or memory access ordering, choosing between alternative expressions those that are more inefficient in hardware (e.g. in many modern CPUs accessing data through indices is more efficient than through pointers, but a lot of legacy C programs use pointers to access arrays, under the wrong assumption that this is more efficient), etc.
I agree with the top poster that languages from the ML family are a very good choice for a first-learned programming language.
For a programming language to be known before any assembly language, I believe that even Fortran is much more appropriate than C, because it is much less misleading about how a modern CPU works, and I say this despite the fact that for decades I have been writing programs in the C language, for embedded computers (but before learning the first assembly languages I had experience mostly in Fortran, and to a lesser extent in LISP and COBOL).
For a few years starting with 1990, I liked C very much, because with the Microsoft and Borland C compilers for IBM PC it was a great improvement over the Pascal, Fortran or Basic to which I had access previously, but today, 36 years later, I do not think that there exists any application for which learning or using C makes sense, despite the fact that we will remain stuck with it in many places for many more decades in the future.
Even the ancient Fortran remains more useful than C today, because for many computational applications most modern programming languages are crippled in comparison with it, by poor support for array operations, while C does not have any intrinsic advantage over alternative programming languages. A "C" done in the right way is "D", so it would be better for learning, though it still inherits from C some questionable features, which would not exist in a clean design.
I'm not sure how much influence a First Language has - the biggest impact might be whether someone decides to continue learning to program. I'd also worry that we might lose something if all computer scientists are too similar. There are lisp believers, and there are those who swear by C; and I think the field of CS would be less rich if either were missing. Given that, it's a good thing that there are a variety of languages taught as first languages, and I'd like to see the variety widen (I don't know of any CS degree which begins by teaching Forth).
A common misunderstanding around OCaml is to classify it as a functional programming language, it is actually a multi-paradigm language. In short, in OCaml, you can write in any paradigm you want, imperative, object, or functional.
Typically, I write in a mix of OCaml and stubs in C (for bindings to Linux libraries), for everything, to replace shell scripts, Python scripts, Javascript/Typescript, but also for big applications (I wrote a clone of Emacs in OCaml, a long time ago, before writing MLdonkey).
Once you have started using it, it does not replace your main programming languages, it replaces all the languages you needed...
I personally detest the babel of languages involved in "modern ways of programming" and have settled on C/C++ for everything since they are available everywhere. The only other languages i am willing to seriously consider adding to my repertoire are Erlang (functional/concurrent/distributed/fault-tolerant/hot-reload) and Prolog (knowledge representation and derivation using predicate logic) along with a formal verification language like Lean4/TLA+ etc. due to LLMs usage.
CS3110: OCaml Programming: Correct + Efficient + Beautiful [0]
It is one of the best programming resources that I know of.
It gives an insight into how they thought/think and what we can learn from them.
While watching the series i was stuck by the fact that they often only focused/reiterated the conceptual basics and building on top of them in a step-by-step manner. Whereas if you watch interviews with programmers (even famous ones) you will often see a lot of fancy talk about the complicated features of the language etc.
I believe "Experts" have a way of modeling domain knowledge in their mind in such a way that they intuitively know what is the "wheat" and what is the "chaff". This is what we need to learn. Scheme can do a lot with a minimal language and so can C++ on the other end of the spectrum. So then what constitutes "Programming"? It is simply an expression of Intent via syntax supporting abstraction of state/behaviour.
I remember learning to use OCaml in a properly functional way after so long writing code in C and it was really miserably painful trying to change how I thought about algorithms. I eventually got over the hill and it changed how I write code in C (for the better?), but I do wonder if it would have been easier to have learned OCaml as a first language instead.
But it does say it’s targeted more for an “algorithms” course in the introduction.
As a side note, I think a REPL is the best environment for learning to program for absolute beginners. You get instant feedback and can focus entirely on just short snippets of real code, without having to worry about compilers, build systems, and “the OS” as a whole. Especially if it comes with a GUI wrapper, you can skip the terminal entirely.
Agree with you wrt to REPL, though sooner or later the programmer-wanna-be has to embrace technical sophistication. :)
I looked for Qt bindings but they appeared to be abandoned last time I checked.
Lately, I keep asking myself, do I need to learn this new thing, should I force myself to learn this thing, LLMs know it anyways and so on.
So (asking genuinely), should we learn these things?
The PDF on the other hand gets to use zlib for streaming all the objects. It gets half decent compression.
OCaml is a very strange programming language. I don't think it will be able to sustain e. g. success stories of other languages, such as python.
* Other “pure” hindley-milner languages are tied but among them ocaml has some particular strengths that I’m sure others will discuss.
(I have my own answers, but I'd love to hear yours, too.)
2. OCaml has an idiomatic approach of using interface files for all modules that can be accessed outside their libraries, and interface files give LLMs a great précis of exactly what is wanted. They then just have to follow the types and fill in the blanks to get the implementation. It's nearly the perfect use case.
Rust is fine if you need it but most things don’t and ocaml has more convenient abstractions for “regular” work. If you need rust you need rust but ocaml isn’t that far off in perf.
Haskell type system is too expressive, it itself becomes a place for the agent to make mistakes and get bogged down.
(2) performance
I went through a book similar to the one above, with no internet connection. The first few weeks were rough: I did not quite know what a type was and the compiler error messages were unforgiving and hard to understand without that context. But, once I grokked the core ideas (a proper idea of what could be done with recursion took much longer), things went surprisingly smooth. I definitely credit it with making me a better programmer.
I'm aware of why Haskell is not practical as a production language for most companies, but I have to say I've never really coded in anything else that feels as "neat" and it's a shame. Every other language feels like it has some idiosyncratic scaffolding one has to learn, reminding you that you're constrained by how computer hardware works, rather than just expressing an algorithm in terms of inputs and outputs.
I would say it has made me a better coder, I've still kept a preference for keeping data immutable, copyable and abstracting complexity into easily testable functions over classes with unobservable mutable state.
Also OCaml lets you write imperative code if you wish so. So you can learn the different paradigms within the same language.
Caml was how I was taught computer science in prépa and first few years of engineering school, a mere 25 years ago.
(Ok, I had done bits of BASIC before, but I had time to recover.)
To be honest, the learning path was
1. Lots of maths. Then add some more.
2. Algos in pseudo code. (In "French", pseudo code, of course, because, why not ?)
3. Caml as "executable pseudo code". With all the warnings and a hints of disgust as the use of mutation and side effect. (And of course ":=" his completely different from "=", what are we, beasts ?)
4. Lots of exams where you have to write properly indented programs on paper on the first try to submit all sorts of recursive trees to all sorts of horrendous manipulations - and you can imagine the grader doing the mother of all code reviews
5. Re do that again in engineering school, because a third of the class had done zero computer science, and the other third had learn in Pascal
6. Then learn C and assembler, and get your mind blown in the exact opposite direction
7. See your teachers reluctantly say that "you should just learn java", because "that's what used in the industry", and "no one will ever get a job writing caml anyway"
...
25 years later : yup, some people managed to get jobs writing a dialect of caml for this small startup in a garage serving cat pictures and racist memes to billions of people
26 years later: "you should just learn to prompt LLMs anyway", because "that's what the industry needs, and no one will get a job programming any more"
No wonder I failed this sht, kind of.
I still think it worth my time kneading dough by hand. It teaches me the various properties of flour, how external factors like humidity or temperature impact the overall process, and I believe that it makes me a better baker, even when I use a machine, because I am better at controlling what the machine does. When I get a new brand of flour I will make sure to bake everything by hand first to "get a feel".
Kneading by hand is also very relaxing to me. This is probably the main reason I bake in the first place.
Programming, and other activities are not very different. We now have machines that can do it faster, at a fraction of the quality many people deem good enough. If you hate coding, that is probably all you need to use, and learning a new language might just be a frustrating experience not worth subjecting yourself to. But if you enjoy coding then it should make you better at it, even when you use the machines.
[1] https://www.cs.cornell.edu/courses/cs3110/2025sp/
[2] https://pragprog.com/titles/tpp20/the-pragmatic-programmer-2...
The answer is still the same as well, people learn ocaml either because they enjoy it, or because learning a functional language makes them a better programmer overall and teaches your brain to approach a problem in a different way.
An oblique explanation: https://croissanthology.com/earring
More recently, you had to go to the store and buy it, which meant you didn't have much variety.
Today, learning an instrument is for social status, inheriting the shine of the past, where music was rare and costly. The reason to learn an instrument today is because the former situation was romanticized.
It'll probably take a generation before people ease into guilt-free enjoying infinite, fully generated music.
https://tenor.com/view/tf2-wall-e-team-fortress-2-autobalanc...
But what about the future generations skipping the step of learning the OCaml's, the C's, the Python's...? It's quite concerning.
Oh by the way, yes. Learn OCaml!
I wanted to learn a functional programming language with powerful type capabilities and I chose the Lean Language for that and not OCamel or Haskell. Reason being: Better type system (dependent types!), applicable in formal domains and can use it to learn math too.
For your bread and butter programming, there is already JS/Go anyways.
So don't see much point in learning OCamel.
That being said, you may as well use Rust. The extra complexity of manual memory management and Rust idiosyncracies are easily dealt with by the LLM.
Also, it may very well be the decade of formal verification - if so, OCaml is a good place to be.
Humans are the ones who Understand, while LLMs only Know.
So inter-disciplinary/cross-disciplinary insights, new modes of thinking/reasoning, flashes of insight etc. are still in the purview of Humans only. AI/LLMs can help focus and short-circuit the study of various subjects but their understanding can only happen within a human "Mind". If you do not even have basic domain knowledge (i.e. unknown unknowns) how can you even prompt/query an LLM for answers?
A few illustrative examples; a) Newton came up with limits/calculus out of a need to measure continuous motion with varying speeds b) Kekule came up with the benzene ring from a dream where he saw a snake grab its own tail c) Descartes came up with the cartesian coordinates in an attempt to solve geometry via algebra etc. Each of these was a novel leap of insight bringing together various concepts to create entirely new knowledge domains.
So one should learn/study the core concepts/ideas in various domains and then push the tedious mechanical labour onto the machines. In this regard see also the concept of "Active Learning" - https://en.wikipedia.org/wiki/Active_learning
Hhhmmmmm
Personally I try to stick within my own niche though - ruby, java and also python (ruby is unfortunately losing grounds really hard now, the writing was on the well in the last some years though, and people such as DHH are now indeed a liability rather than an asset to be had, but that's a side topic).
I think what LLMs will force in the long run is to make programming languages used by real humans in a traditional way, more effective. That is, writing code by humans will have to become a lot more efficient, both time-wise and speed-wise. And for that there is always a use case IMO since LLMs are, despite the promo, incredibly stupid.
Lifetimes are a compiler safety abstraction and mostly unrelated with how the computer runs your program.
Learning a language like C helps to understand why lifetime annotations are needed and how the compiler uses them.
This is assuming that the current state of LLMs is sustainable, which it definitely isn’t.
For Linux UI.
This is exactly what makes it great for teaching. Students don’t need to know actual arch or hardware details. They just need to grasp the core concepts of what is happening on the hardware.
For that purpose the ideal teaching language is lower level than python, javascript, Ocaml, etc without diving into nitty gritty arch specifics.
C is the undisputed champion in that domain.
C really isn't as great for this as is often suggested either, not for decades at least
K&R's original compiler on an actual Digital machine from that era makes the case best, but remember this is the era when if you hot loop over modifying a variable your compiler is going to emit memory stores for each iteration - because that's what you wrote, isn't it? No modern C compiler would do this because it's awfully slow.
Likewise that iteration of C doesn't have what you'd recognise as function prototypes, it doesn't care whether your function takes six arguments, here are six arguments the first two are integers, good luck with that. In assembler that makes sense, but you don't do that in modern C either.
C is still a close-to-the-metal language, but it is programming an abstract machine and it is important that the programmer knows that's not really how the machine works, if you want to learn about the machine you will need to write at least assembler and possibly just go learn electronics. Good luck.
It's good enough for undergraduate teaching. In C, you can easily explain the relation between a struct definition and its layout in memory. It's much more difficult in Caml (what's the relation between an algebraic datatype and its layout in memory?) or Java (which introduces pointers that you never asked for).
We (University of Paris-Cité) are teaching Java in first year, then C and Caml in second year, with seemingly good results.
Most modern languages have ways to automate parts of memory management, and rightfully so. But you should at least be somewhat aware of what is going on under the hood.
What low level thing do you think C can do, but Modula-2, Ada or Object Pascal are unable to provide the comparable feature?
Also taking into account possible C extensions not covered by ISO.
It’s also popular enough that there is an abundance of high quality learning resources for beginners.
No other language threads that needle as well as C.
I would rate them as about 5% true and 95% false, as explanation of the past and prediction of the future.
The satisfaction of learning to do something difficult isn't going away, and the social status associated with it won't either.
Lol. The reason to learn an instrument is because it is directly pleasurable to play an instrument. You've got consumer/spectator brain.
I mean, why post a comment when you could have just read a comment?
the average person spends 6(!) hours a day on their smartphone, the average TikTok user spends 100 minutes on the app alone. This isn't about artificial scarcity, it's about the average person looking like the Wall-E people
People who are asking these questions are saying "will me spending my time learning OCaml help me land that job that pays six figures and has health insurance so I can not rot away living on the margin". They aren't saying "I only do things that will make me money".
again, it's not a small window. It's six hours. That's almost half your waking day. People spend virtually their entire leisure time rotting away on low quality entertainment.
>will me spending my time learning OCaml help me land that job
that's a pointless question for one you never know if something will land you a job, new opportunities don't open up before you do something, secondly the relevant question is, should I stop doing X and start learning Ocaml, or Chinese, or take a welding class because all of that even if it doesn't work out beats scrolling through Instagram.
I don't even take offense with the idea that you engage in activity that makes you money, because pure selfishness on that front would be an improvement to what most people are doing now.
They had originally planned on june and my part is in so…. Me too.
Likewise packing isn't allowed by the standard, so you'd again only need to talk about packing if you want to.
This seems like a reasonable place to start. Like the way driving school teaches you a U-turn but not a J-turn. Is a J turn actually a thing you might need? Maybe, but it's definitely not where we should start.
Also somehow the implied argument that computing hardware and operating systems simulate a PDP-11 for the sake of C is completely backwards. Historically, there were other approaches, e.g. processors designed for object-oriented programming or actors etc.. All those were not very successful.
UNIX for PDP-7 in Assm -> UNIX for PDP-11 in Assm -> UNIX for PDP-11 in C
https://news.ycombinator.com/item?id=42644851
and
Those that don’t need low level details can spend their professional career in languages like python and JavaScript while still have a sense of what lies beneath the abstraction.
For those that do want or need to go deeper, C is an excellent jumping off point into ASM and arch specifics.
As for OCAML vs not, I think the vast majority of even intellectual and studious people would be better served trying to AI max and build some kind of agent serving businesses than trying to get a job at, uhh, Jane Street. 1% of the best engineers in the world get to work in that language, so yeah the parent makes a valid point
A. you reduce impedance mismatch, but making the new language using similar concepts and mechanisms
--
You're right, this table is just a coincidence, probably derived from the standard math notation, and used in many PLs like FORTRAN and Python ;)
PDP-11 C
INC R ++i
DEC R --i
ADD src, dst dst += src
SUB src, dst dst -= src
(R)+ *p++
-(R) *--p
X(R) p[x]
@(R)+ **pp++
@X(R) *p[x]
BR label goto label ; near jump
JMP label goto label ; far jumpIt would also completely contradict the whole idea that everything today simulates the PDP-11 design because of C, as few architectures have this auto-in/decrement addressing modes despite C having native syntax.
PDP-11 was hugely influential in the later designs of various Hardware, Software, OS, Languages etc. See for example; Dave Cheney's What Have We Learned from the PDP-11? - https://dave.cheney.net/2017/12/04/what-have-we-learned-from... The conclusion from the article;
While its development was sometimes chaotic, and not without its flaws, the PDP-11 is at the intersection of many threads of history.
Hardware, software, programming languages, operating systems, have all been influenced by the PDP-11. I wager there is not a single person in this room who cannot trace the lineage of the language they work with, the computer they use, or the operating system it runs, back to the PDP-11.
And that is worth celebrating.
While the PDP-11 instruction set was certainly influential in the design of the "C Abstract Machine" the latter was generalized to accommodate other architectures extent at that time (eg. Honeywell 6000, IBM System/370) with enough flexibility that you can implement a C compiler for almost any architecture you can think of. That is its strength.
David Chisnall's criticisms in his C Is Not a Low-level Language: Your computer is not a fast PDP-11 (https://queue.acm.org/doi/10.1145/3212477.3212479) has to do mainly with the fact that the abstract machine was serial execution with no concept of memory protection/models. But this very flexibility is what makes C easily portable to dinky little MCUs which do not have those features while allowing the programmer to explicitly program those using libraries on more complex processors with lots of parallel cores, mmus etc.
Thus a single thread runs on a "C abstract machine" on a core (i.e. the bare minimum) and it is up to the programmer to manage interactions between the threads on various cores. We have lost nothing but perhaps burdening the programmer with more knowledge of hardware complexity which was an acceptable tradeoff then. Note also that there already exists various extensions to C to handle parallel programming directly eg. "Concurrent C" by Narain Gehani et al. The industry however chose to settle on external libraries and optional thread support in C11 again keeping with its minimality and flexibility mantras.