4 ms·
I will definitely be keeping an eye on Nim. That aside, does anyone else find the fact that Nim treats "-1" differently to "- 1" (quotes added for clarity) a bi
by SeanDav 11y ago
I will definitely be keeping an eye on Nim. That aside, does anyone else find the fact that Nim treats "-1" differently to "- 1" (quotes added for clarity) a bit disconcerting?
- adrusi 11y agoThat depends on how good the error messages are. I can't see it being a common problem, but if there was a typo, I can imagine it being quite hard to find without an error message clearly indicating.
- RodgerTheGreat 11y agoNegative literals/negation/subtraction sharing "-" leads to odd corner cases in the grammars of quite a few languages. I'm rather fond of the J approach: -1 is the application of the negation operator to the literal one. _1 is the literal negative one. This is a very important distinction in J since you can have whitespace-delimited lists of numbers. "-1 2 3" is equivalent to "_1 _2 _3".
- oinksoft 11y agoThis is also true of Standard ML, where unary minus is ~, and APL, where unary minus is ¯
- pklausler 11y agoTo be most clear: The "high minus" in APL is not the unary negation function; it's part of the syntax for a negative numeric literal. If X is 1, "-X" computes "¯1", and "¯X" is ill-formed.
- klibertp 11y agoIn Lisps, this: (- 1) and this: (-1) Are completely different things (the latter is actually an error). Also in LiveScript ls> (- 2) [Function] ls> (-2) -2 Also in Smalltalk, this: -3. "=> -1" - 3. "=> Error!!!" That's off the top of my head, I'm sure there are many other languages with this characteristic. Why would that be a problem? It's just a single (or a couple) parse rules to remember (but you get other benefits from it).
- PeCaN 11y agoPerhaps most notably, Haskell has the same behavior as LiveScript. EDIT: Derp, that's not true. (- 2) == (-2)
- pklausler 11y agoTo be clear: (- 2) is not a section in Haskell, it's effectively a numeric literal because '-' is Haskell's only prefix operator. Haskell has a 'subtract' function that's defined in its Prelude that is used where a section for (-) would be useful. It's a minor wart, and probably never going to be fixed. EDIT: clarified
- vram22 11y ago>Are completely different things (the latter is actually an error). Why is the latter an error?
- klibertp 11y agoIn Lisp, a parenthesized expression is a function call by default, if not explicitly transformed in some way (for example with `quote`). `(f 1 2 3)` would be written as `f(1, 2, 3)` in other languages, and `(f)` would be `f()`. So `(-1)` would translate to `(-1)()` which doesn't make sense. Well, not exactly, as for example in CL and Elisp you have a `1+` function[1], which... well, adds 1 to its argument. The difference is that `1+` is not a valid literal number, so it's read as a symbol, and if this symbol has a function attached to it, it is called. In Common Lisp you can work around this by using a special syntax: `|-1|`, which forces reading the thing inside as a symbol. You can then define functions and call them this way: (defun |+1| (x) (1+ x)) (|+1| 1) ;; => 2 [1] `1-` too, of course.
- vram22 11y agoThanks for the answer. I know very little Lisp, though I find it interesting. > `|-1|` Is there a name for that | operator - if it is one?
- JadeNB 11y agoA moment's thought failed to turn up examples—which is almost certainly my fault. What is a context in which both could appear? In that context, what other languages treat them identically? EDIT: Ah, the documentation mentions: > This also means that `-1 is always parsed as prefix operator so code like `0..kArraySize div 2 -1` needs to be changed to `0..kArraySize div 2 - 1`.