Another interesting way to think about it is as "match". That is try to match the stuff on the right with the stuff on the left. Take Erlang for example: 1> X = 1. 1 2> X = 2. ** exception error: no match of right hand side value 2 Notice variables are immutable (not just values themselves). Once X becomes 1, it can only match with 1 after that. You might think this is silly or annoying, why not just allow reassignme…
Elixir has borrowed the pattern matching approach from Erlang, but allows rebinds which makes code refactoring easier. iex(1)> x = 1 1 iex(2)> x = 2 2 iex(3)> ^x = 3 (MatchError) no match of right hand side value: 3 Notice the pin operator (^) to achieve the same effect.
Why Does “=” Mean Assignment?
131–140 of 367 posts
Re: Why Does “=” Mean Assignment?
#132Because programming languages are typically designed for the tiny group of existing programmers than the much larger group of future programmers. The same reason unnecessary tokens exist, and 'drop' means delete in databases. It's entirely cultural.
So this is a dilemma I have while working on a new language. I'd like to go with `:=`, but `=` is absurdly popular, and I'm trying to keep the language as approachable as possible.
I don't think the clarity of `:=` is so compelling that it outweighs the `ew, why are there colons in there` reaction that I think most novice coders would have.
(Remove redundant commentary about database stuffs.)
Re: Why Does “=” Mean Assignment?
#133https://en.wikipedia.org/wiki/Relational_operator#Confusion_...
but Wikipedia says: "The reason for all this being unknown. [footnote] Although Dennis Ritchie has suggested that this may have had to do with "economy of typing" as updates of variables may be more frequent than comparisons in certain types of programs"
while the OP says: "As Thompson put it:
Since assignment is about twice as frequent as equality testing in typical programs, it’s appropriate that the operator be half as long."
Thompson and Ritchie designed C together.
Re: Why Does “=” Mean Assignment?
#134Because K&R had terrible keyboards so they abbreviated everything as much as possible. Traditionally := was used for assignment, which makes sense since it is an asymmetric symbol for an asymmetric operation.
Probably more important is that the Unix/C developers came from Multics, written in PL/I, which (following Fortran) used ‘=’ for assignment. And Fortran was still important; Unix had a Fortran compiler at least as far back as Second Edition, when C was being born. Kernighan & Plauger's Elements of Programming Style used Fortran and PL/I for its examples, and Software Tools used Ratfor. ‘=’ is simply what they were used to.
Re: Why Does “=” Mean Assignment?
#135I always liked DHH's take on these sorts of arguments (paraphrasing): who the hell cares? Once you know the purpose of the '=' how often do you make mistakes reading or writing code? Whereas Java is all about protecting developers from themselves, Ruby (for example) let's you get away without variable type declaration because at the end of the day, how often do you not know whether a particular variable is a string o…
I'm refactoring some data-pasta to more clearly declare types because we just have dicts of lists of whatever, so often enough that a random reader chimed in after 20 minutes.
Re: Why Does “=” Mean Assignment?
#136Funnily, when Thompson was asked what he would do differently if he were doing it over again, he said, "I'd spell creat with an e."
Re: Why Does “=” Mean Assignment?
#137Re: Why Does “=” Mean Assignment?
#138I always liked DHH's take on these sorts of arguments (paraphrasing): who the hell cares? Once you know the purpose of the '=' how often do you make mistakes reading or writing code? Whereas Java is all about protecting developers from themselves, Ruby (for example) let's you get away without variable type declaration because at the end of the day, how often do you not know whether a particular variable is a string o…
Re: Why Does “=” Mean Assignment?
#139Earlier quoted context omitted.
There’s also <-, most commonly seen in R but draws it’s heritage from the APL keyboard
Common in Pre-Python pseudo code and algorithms.
Re: Why Does “=” Mean Assignment?
#140Another interesting way to think about it is as "match". That is try to match the stuff on the right with the stuff on the left. Take Erlang for example: 1> X = 1. 1 2> X = 2. ** exception error: no match of right hand side value 2 Notice variables are immutable (not just values themselves). Once X becomes 1, it can only match with 1 after that. You might think this is silly or annoying, why not just allow reassignme…
Also valid in Erlang: 2 = X. {ok, Y} = X = foo(). Oh, and here's a fun one: 1> {X, X} = {1, 1}. {1, 1}. 2> {Y, Y} = {1, 2}. ** exception error: no match of right hand side value {1,2} If you're wondering what the use of that is, here's an Erlang "drop all occurrences of an element X from a list" function. (Prerequisite knowledge for this: Erlang functions have several clause-heads; which one is executed on each call…
drop(X, List, Result) :- drop(X, List, [], Result).
drop(_, [], Acc, Result) :- reverse(Acc, Result).
drop(X, [X|Rest], Acc, Result) :- drop(X, Rest, Acc, Result).
drop(X, [Y|Rest], Acc, Result) :- dif(X, Y), drop(X, Rest, [Y|Acc], Result).
I only had to make a few syntactic changes to the original program to obtain a Prolog predicate from it. The most significant change is that I use an additional argument to hold the original function's return value. An important consequence of the resulting relational nature is that this can answer quite general queries.For example, we can ask: What are possible solutions Ls that arise when we drop X from the single-element list [A] ?
?- drop(X, [A], Ls).
X = A, Ls = [] ;
dif(X, A), Ls = [A].
We see that there are two possible answers: One where X is A, and hence the result is the empty list []. And the other where X is different from A, as indicated by dif(X, A).Even more generally, we can systematically enumerate all conceivable solutions for all list lengths:
?- length(Ls0, _), drop(X, Ls0, Ls).
Ls0 = Ls, Ls = [] ;
Ls0 = [X],
Ls = [] ;
Ls0 = Ls, Ls = [_586],
dif(X, _586) ;
Ls0 = [X, X],
Ls = [] ;
Ls0 = [X, _598],
Ls = [_598],
dif(X, _598) ;
etc.
This uses iterative deepening to generate all possible answers.