Live data from Hacker News

Why Does “=” Mean Assignment?

hillelwayne.com

251–260 of 367 posts

Re: Why Does “=” Mean Assignment?

#251
post #236
post #140

Earlier quoted context omitted.

To illustrate how close Erlang syntax is to Prolog, here is drop/3 in Prolog: 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…

Also not stuck using only an unbound result. Could we not ask "given L and Ls, what elements were dropped?"

Yes, that's also possible with this definition! For example: Given Ls0 = [a,b,c] and Ls = [a,c], which element (if any) was dropped:

    ?- drop(X, [a,b,c], [a,c]).
    X = b ;
    false.
As another example, if both Ls0 and Ls are [a,b,c]:

    ?- drop(X, [a,b,c], [a,b,c]).
    dif(X, c),
    dif(X, b),
    dif(X, a).
This means that X must be different from each of these elements.

More generally, which elements can be dropped at all from the list [a,b,c]:

    ?- drop(X, [a,b,c], _).
    X = a ;
    X = b ;
    X = c ;
    dif(X, c),
    dif(X, b),
    dif(X, a).
Interestingly, the predicate definition does not even use "=" in its clauses. In Prolog, (=)/2 is a built-in predicate that means unification. In the definition above, we use implicit unification instead of explicit unification.

Re: Why Does “=” Mean Assignment?

#252
post #5

Because 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.

I think the simple reason is that two characters are harder to type then one character, if they both are a key combo. And to keep syntax more clean.

Re: Why Does “=” Mean Assignment?

#253
Coincidentally I am just reading the >, and found this in the introduction chapter:

> C history ... The essence of BCPL was the array and pointer model which abandoned any hope of strong typing and (with hindsight) a proper mathematical model of the mapping of the program onto a computing engine. Even the use of := for assignment was lost in this evolution which reverted to the confusing use of = as in Fortran. Having hijacked = for assignment, C uses == for equality thereby conflicting with several hundred years of mathematical usage. About the only feature of the elegant CPL remaining in C is the unfortunate braces {} and the associated compound statement structure which was abandoned by many other languages in favour of the more reliable bracketed form originally proposed by Algol 68. It is again tragic to observe that Java has used the familiar but awful C style.

Re: Why Does “=” Mean Assignment?

#254
post #245
post #127

Earlier quoted context omitted.

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…

I have mixed feelings on this. On one hand I did want this notation while learning haskell. Partly because there are a bunch of places in haskell's type system where repeating a variable implicitly gives equality constraints. On the other hand something like drop _ [] = [] drop x (y:xs) | x == y = drop x xs | otherwise = y : drop x xs is often more readable at a glance to me since I don't have to parse variable names…

The inferred type of

    drop _ [] = []
    drop x (x:xs) = drop x xs
    drop x (y:xs) = y : drop x xs
should be

    drop :: Eq a => a -> [a] -> [a]
of course.

Re: Why Does “=” Mean Assignment?

#256
post #2

The '=' sign is used because after that statement the variable will indeed have equality with the r-value. Coincidentally in async languages like Verilog there is a another assignment operator '<=' which means that the variable won't take the new value until the next clock cycle (or more explicitly the next evaluation of the process block). '=' exists also and has the same meaning as with traditional languages.

What about `a = a + 1`?

Re: Why Does “=” Mean Assignment?

#257
post #5

Because 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.

> Because K&R had terrible keyboards

There's no K in Thompson & Ritchie.

Kernighan seems to get inserted all over the place regarding Thompson's work. It's a bit disrespectful to the legend himself.

Re: Why Does “=” Mean Assignment?

#258
post #233

Earlier quoted context omitted.

What does prevent : to become the assignment operator of a language? It's short, and quite "explicit". x: 2

The Rebol family of languages works like that.

Except that it doesn't. ":" in "x:" is not an assignment operator and "x" is not a variable.

Re: Why Does “=” Mean Assignment?

#260
post #152

The third option: an assertion of fact A language could reasonably use plain = to let the programmer state that two things are equal. In a debug build, this could be compiled into code that validates the condition. A debug build would abort if the condition is not met. For a performance-optimized build, the compiler could use the information for optimization. It could be like the __assume keyword that Microsoft suppo…

This is essentially what ‘assert’ does in some C like languages except it allows more than just asserting equality. For example:

  assert(length 
Post reply on HN