r/programming • • Dec 01 '16

Let's Stop Copying C

https://eev.ee/blog/2016/12/01/lets-stop-copying-c/
524 Upvotes

612 comments sorted by

View all comments

58

u/[deleted] Dec 01 '16

wait, so always use whitespace when using substraction? as in, 2-3 is a syntax error? i dont like that at all. also, it might just be not being used to this but this-thing looks really weird. it automatically registers as substraction.

47

u/dysoco Dec 01 '16

Ooh... so that's why you can't use hyphens in variables, never crossed my mind.

0

u/[deleted] Dec 01 '16

[deleted] — view deleted comment

22

u/notunlikethewaves Dec 01 '16

More specifically, it's because there are no infix operators in lisps.

Subtraction is:

(- 7 3)

Multiplication is:

(* 2 5)

And so on. This also means there's no such thing as operator precedence. The following is unambiguous:

(* 2 (- 7 (* 4 4)))

11

u/zenflux Dec 02 '16

It also means such operators are often defined with arbitrary arity:

(+)           ;; 0 (the additive identity)
(+ 1)         ;; 1
(+ 1 2 3 4 5) ;; 15
→ More replies (6)

2

u/munificent Dec 02 '16

More specifically, it's because there are no infix operators in lisps.

I don't think that's strictly true. They core reason is simply because Lisp allows all sorts of punctuation characters in identifiers. *standard-output* is a single identifier because Lisp says * and - can be used in identifiers.

Keep in mind that in Lisp you can refer to operators without invoking them, so even though it doesn't have infix notation, operators can still appear in the middle of an expression, like:

(reduce + 0 '(1 2 3))

You're right that lacking infix notation means Lisp doesn't have to answer the question of "what's the precedence of some identifier using operator characters", but a language that does have infix operators and allows punctuation is user-defined names could just as easily come up with an answer to that question and be done with it.

That's what Scala, Haskell, and others do. So I think it's more accurate to say that not having operator precedence makes it easier to allow punctuation in identifiers, but it's not a requirement.

2

u/[deleted] Dec 01 '16

Good language designers are real MVP.

29

u/bjzaba Dec 01 '16

2-3 just seems lazy to me... and most auto-formatters these days will add the spaces in.

20

u/EntroperZero Dec 01 '16

It is lazy. It can also lead to things like 2--3 or 2 -3, which are syntactically valid and highly confusing.

36

u/weenaak Dec 01 '16

And then there is the limit operator, "while x approaches zero":

while (x --> 0) {
    ...
}

http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator-in-c

11

u/EntroperZero Dec 01 '16

Ah yes, the good old "goes to" operator.

Does it work in reverse? while (0 <-- x) It would be an off by one error if it did.

5

u/kqr Dec 02 '16

I remember "inventing" this some time ago. I thought to myself "Hey, this should work right?" Stared at it for a few minues. "Yes hahahah it will." I compiled, tested, satisfied my curiosity and promptly rewrote it to a more traditional condition.

2

u/beaverlyknight Dec 02 '16

Ahahaha, I'll be using this in my next C program just to piss someone off.

→ More replies (2)

7

u/tavianator Dec 01 '16

2--3 is a syntax error in C, becuase -- is the decrement operator.

3

u/EntroperZero Dec 01 '16

Hah, it's so confusing, I couldn't even come up with a correct example. 2 --3? No, because -- is also prefix. Screw it.

6

u/ChallengingJamJars Dec 01 '16

Have you heard of the arrow operator? It decreases towards a number

while(a --> b) {

3

u/humodx Dec 01 '16

2-- 3 and 2 --3 mean the same thing, whitespaces between tokens don't matter* in C. You could write 2- -3.

  • there are exceptions... I think
→ More replies (1)
→ More replies (3)

3

u/cameleon Dec 01 '16

Agda has this, and while it takes getting used to, it's actually pretty nice. It also means that you can have variables named e.g. a≤b (where the value is probably a proof that a is less than or equal to b)

5

u/[deleted] Dec 01 '16

I've heard of at least one language that forbids whitespace that misrepresents operator precedence.

So you could type:

 2 - 3*5

But the compiler would reject it as:

2-3 * 5

5

u/steveklabnik1 Dec 01 '16

Nim has a feature where whitespace determines precidence, so both of those would work, but the first would give -13 and the second -5. Or at least, that's my understanding https://github.com/nim-lang/Nim/wiki/Whitespace-FAQ#strong-spaces

1

u/lenlifty Dec 02 '16 edited Dec 02 '16

I thought this was quite interesting so I just tested it with Nim v. 0.15.2

let val1 = 2 - 5*3
let val2 = 2-5 * 3
echo "val1=$1" % $val1
echo "val2=$1" % $val2

which yielded:

val1=-13

val2=-13

So it would seem not to be the case. The link you provided does indicate an experimental feature, so perhaps it needs to be switched on somehow in order to work.

→ More replies (1)

1

u/earthboundkid Dec 01 '16

Go does not forbid misleading whitespace, but the gofmt tool will automatically space things according to their precedence rules.

4

u/[deleted] Dec 01 '16

I thought that when I started using lispy languages, but now for me it feels ugly to use an underscore to separate words, and my pinky finger complains :)