4 ms·
When you put parentheses around an atom, lisp sees that as a function call. Say `x` is a function which adds one to a number. Say `y` is an atom which is not
by nickloewen 7y ago
When you put parentheses around an atom, lisp sees that as a function call.
Say `x` is a function which adds one to a number.
Say `y` is an atom which is not a function—perhaps a list: `(list 1 2 3)`.
If you write just `x`, no parens, you point to the function without executing it. If you do this at your interpreter it will tell you that the value of `x` is “function something something.”
This is important because it’s what makes it possible to pass functions as arguments, and write things like `(map x y)`. Here `x` is not evaluated in place; it’s called later by the `map` function, once for each element of the list, resulting in `(list 2 3 4)`.
Now if you put parentheses around `x`, lisp will try to execute it on the spot. `(x 1)` is what other languages would call `x(1)`, and will return `2`.
You have to be a little careful about this because it means that if you put parens around `y`, which isn’t a function, you’ll get an error. This is why lisp has the ‘quote’ operator, which means “take the next atom as literal data and don’t try to execute it.” So `(quote (1 2 3))` will return `(1 2 3)`. This can also be written `’(1 2 3)`—which is nice, because this is effectively equivalent to `(list 1 2 3)`, and could have saved me some typing when I defined `y` earlier.
e: typo ; also, sorry for the ugly syntax, I forgot there was no inline code formatting on HN
- milhnl 7y agoThe inline syntax makes it clearer what you mean. After some time markdown reads as text. Thank you!
- dreamcompiler 7y agoHold up. X in the above is a symbol. Symbols may be bound to a function or they may not. But x here is always a symbol. When you write (x) in the Lisp REPL, Lisp will assume you intend to call a function bound to x because x appears first in the list. If you instead wrote (y x), Lisp would never assume x was a function. If the first thing in a list is not bound to a function, Lisp will throw an error. When you write '(x) [note leading quotation mark], Lisp will simply construct a list that contains the symbol x as its only element. In my explanation above, I didn't make the distinction about the quotation mark because I assumed the questioner was asking about data structures. Lisp will print "the list containing x" without the quotation mark. But if that's what you mean when you type it, you have to explicitly type the quotation mark.
- nickloewen 7y agoYes, this is a good clarification!