4 ms·
In 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 wr
by klibertp 11y ago
In 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.