Live data from Hacker News

Learn Programming with OCaml

usr.lmf.cnrs.fr

141–150 of 150 posts

Re: Learn Programming with OCaml

#141

I believe that an ML should be the First Language for Computer Scientists. It's much less clear which language we should teach people for whom it will likely be their Only Language, today Python is common, and for fields which care about stats often R is used, I've also seen Java used in this role. But for a First Language specifically I am in no doubt that an ML should be chosen, and this is despite the fact that th…

An ML is great for the theory or abstraction side of programming, but C is also hands down the best language to get a sense of how the computer is running your program. 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.

To get a sense of how the computer runs your program you need to learn some modern assembly language, i.e. either Intel/AMD x86-64 or Arm Aarch64, or for something simpler one could start with IBM POWER.

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.

Re: Learn Programming with OCaml

#142
post #134

Earlier quoted context omitted.

Note that IMHO there are quite a few things incorrect or misleading in this article.

Do you mean in Cheney's article or Chisnall's article or in my comment?

Chisnall's article. A lot of the general criticism would still apply if you replace "PDP-11" with "von Neumann architecture" which makes the whole thing a bit weird. But also many specific comments about C are bit off (the cost of copying padding, the sequencing, provenance, etc.)

Re: Learn Programming with OCaml

#143
post #137

Earlier quoted context omitted.

All great options as well, with the caveat that it’s possible to “ignore” the parts that make C useful as a teaching tool. 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.

Some of the suggested languages are as old as C, with better ergonomics if you mean ignorig is doing string concatenation instead of manually handling char buffers with pointer arithmetic, or proper reference parameters instead of being forced to pass the address of output variables manually, which is nonetheless possible in the suggested languages. What low level thing do you think C can do, but Modula-2, Ada or Obj…

C lacks just enough “modern language features”, by design, that students are forced to be vaguely aware of memory layout, memory management, ref vs copy, shallow copy vs deep copy, maybe even calling convention, etc.

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.

Re: Learn Programming with OCaml

#144
post #137

Earlier quoted context omitted.

Some of the suggested languages are as old as C, with better ergonomics if you mean ignorig is doing string concatenation instead of manually handling char buffers with pointer arithmetic, or proper reference parameters instead of being forced to pass the address of output variables manually, which is nonetheless possible in the suggested languages. What low level thing do you think C can do, but Modula-2, Ada or Obj…

C lacks just enough “modern language features”, by design, that students are forced to be vaguely aware of memory layout, memory management, ref vs copy, shallow copy vs deep copy, maybe even calling convention, etc. 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.

Provided examples from 1978 and 1983 languages do that just as well, unless you mean by 1978, C design was too primitive, or even when compared with languages like PL/I and NEWP from 1966 and 1961 respectively, which then I fully agree with.

Re: Learn Programming with OCaml

#145

I believe that an ML should be the First Language for Computer Scientists. It's much less clear which language we should teach people for whom it will likely be their Only Language, today Python is common, and for fields which care about stats often R is used, I've also seen Java used in this role. But for a First Language specifically I am in no doubt that an ML should be chosen, and this is despite the fact that th…

An ML is great for the theory or abstraction side of programming, but C is also hands down the best language to get a sense of how the computer is running your program. 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.

You really need to grok C and understand Lisp. Learning just these two well, will give you most of the foundation you're ever gonna need. Nearly every other PL is a bunch of accretions on top of one of these; sometimes those accretions are great improvements, and more often than you'd think - they are downgrades.

Lisp can teach you everything about what logically a program is, how computation itself can be expressed - code as data, recursion, and abstraction built from almost nothing, and C can teach you what a program physically is - memory, pointers, and how the machine actually runs it.

I have gone through over a dozen of different PLs, each time thinking that my skillset was improving but I still sucked at it, and just needed to learn yet another language to truly get better at programming. Turns out, I just needed to grok Lisp.

Don't be like me, don't waste your lives wandering around, following a map everyone's holding. Majority not only can be wrong, but as history teaches us - it is wrong most of the time. Instead, follow those few that are looking for shortcuts. They may not have most popular opinions, but they might be on the right path after all.

Re: Learn Programming with OCaml

#146
post #142

Earlier quoted context omitted.

Do you mean in Cheney's article or Chisnall's article or in my comment?

Chisnall's article. A lot of the general criticism would still apply if you replace "PDP-11" with "von Neumann architecture" which makes the whole thing a bit weird. But also many specific comments about C are bit off (the cost of copying padding, the sequencing, provenance, etc.)

Right. Its catchy title gained it notoriety and then got amplified by human parrots.

However, it is a good article to read and understand low-level multiprocessing issues.

Re: Learn Programming with OCaml

#147
post #128

Earlier quoted context omitted.

Fully agree. C sits in an almost perfect level of abstraction for students to get a vague sense of how the computer runs code. 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.

C hasn't presented an accurate model of how computers actually work for decades. It's a high level language LARPing as a low level language.

Computers haven't presented an accurate model of how computers actually work for decades. Compare the surface ISA of anything mainstream since the mid 90s versus all of the superscalar and out-of-order execution happening under the hood.

Deliberate efforts to surface this deeper layer back to the programmer-facing ISA (VLIW, notably Itanium) failed spectacularly outside of niche applications. C and its contenders can't simply hop over the CPU abstraction presented to them without having to map backward from some lower-level model to the surface ISA it's forced to target then having the rug pulled every few years due to microarchitectural changes. It has managed to evolve surprisingly gracefully over decades from tiny 16 and 18-bit machines, sometimes segmented, to 32 and 64 bit machines, and even accommodating SIMD reasonably gracefully via intrinsics (which is about the best we can do at present, as CPUs continue to diverge wildly in this area, though I'm begrudgingly impressed by what modern compilers can do via autovectorization to optimize simple loops).

Re: Learn Programming with OCaml

#148
post #142

Earlier quoted context omitted.

Do you mean in Cheney's article or Chisnall's article or in my comment?

Chisnall's article. A lot of the general criticism would still apply if you replace "PDP-11" with "von Neumann architecture" which makes the whole thing a bit weird. But also many specific comments about C are bit off (the cost of copying padding, the sequencing, provenance, etc.)

You might find Beyond the PDP-11: Architectural support for a memory-safe C abstract machine by David Chisnall et al. also interesting - https://dl.acm.org/doi/10.1145/2786763.2694367

Re: Learn Programming with OCaml

#149
post #142

Earlier quoted context omitted.

Chisnall's article. A lot of the general criticism would still apply if you replace "PDP-11" with "von Neumann architecture" which makes the whole thing a bit weird. But also many specific comments about C are bit off (the cost of copying padding, the sequencing, provenance, etc.)

You might find Beyond the PDP-11: Architectural support for a memory-safe C abstract machine by David Chisnall et al. also interesting - https://dl.acm.org/doi/10.1145/2786763.2694367

Yes, I have much less complaints about the content of this paper, except for the PDP-11 meme aspect.
Post reply on HN