3 ms·
I don't want to diminish your first impressions, however I think you might have misunderstood one or two points in that video. Perhaps I can clarify: Regarding
by themulticaster 5y ago
I don't want to diminish your first impressions, however I think you might have misunderstood one or two points in that video. Perhaps I can clarify:
Regarding 3/"everything is an expression": C does explicitly not have this property, it has statements and expressions. The typical example to illustrate this point is the lack of a true "if expression" in C. [1] Given the following C code:
int x;
if (a > 5) {
x = 1;
} else {
x = 2;
}
The value x is only assigned once, so it would be nice to express this using const. We can achieve this using the ternary operator, but that only works if the if statement is simple enough.
In Rust on the other hand, ifs are true expressions and "return" values, allowing you to write the statement above like this:
(Syntax might be slightly incorrect as I'm writing this without a compiler at hand)
let x = if a < 5 { return 1 } else { return 2 };
From what I'm observing the (functional programming) idea of "everything is an expression" is well-received and also included in recent imperative languages such as Rust.
Regarding 4: Do you mean immutability instead of invariants? I'm not quite sure what you are referring to in the context of Python.
Regarding 7: This was mentioned somewhere else as well, but in a functional programming language you don't have to worry about the compiler recognizing and optimizing tail recursion. The only exception are pathological (extremely and obviously inefficient) function definitions, for example functions that try sum an infinite list of integers and don't terminate or functions that reverse a long list.
[1] Terminology: "if" in C is a statement, and the ternary operator (a?b:c) is an expression similar to "if", but there is no true "if" expression.
- mikewarot 5y agoRe: 3 - I was referring to (a?b:c), which struck me, as a pascal programmer, with horror. ;-) Re: 4 - Immutable strings is the python oddity to which I referred Re: 7 - The video (#4) in the series seemed to indicate that it was almost a requirement that any recursion be a tail recursion.
- steveklabnik 5y ago(You don't use return there, but otherwise, you're correct about the Rust, yes. return would return from the function, not assign the value to x.)
- themulticaster 5y agoWell, this is embarrassing. I'm using a small Rust example to make a point about C oddities, and then a Rust Core team member points out a syntax error in my example! In my defense, I'd say the return keyword - combined with the different semicolon rules - is one of the more confusing and subtle syntactic differences when learning Rust from a C/C++ background. After a few weeks of programming C++ I always use return more often than necessary.
- steveklabnik 5y agoOh absolutely! If you haven’t worked with expression-based languages before, it’s a big mental shift. In my experience it’s harder to come back to one that isn’t after you’ve done things this way for a while. There’s an adjustment period for sure.