5 ms·
In Lisps, this: (- 1) and this: (-1) Are completely different things (the latter is actually an error). Also in LiveScript ls> (- 2) [Funct
by klibertp 11y ago
In 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?
- klibertp 11y agoI didn't find any specific name for it. I think it's just a part of atom syntax, like " is part of string syntax. But it could also be a reader macro - in Common Lisp you can override parts of a parsing process. You can write similar macros yourself. For example, Common Lisp doesn't have a dict object literal ({} in json), but as you can see here: https://gist.github.com/chaitanyagupta/9324402 https://gist.github.com/chaitanyagupta/9324402 you can add it.
- theseoafs 11y agoComparing Nim to Lisps doesn't make much sense. Lists in Lisp are whitespace-separated, so `(- 1)` is different from `(-1)` for the same reason that `(a b)` is different from `(ab)`.