4 ms·
Can you elaborate or maybe link to a resource that explains this? This seems to be missing piece of the puzzle for me.
by milhnl 7y ago
Can you elaborate or maybe link to a resource that explains this? This seems to be missing piece of the puzzle for me.
- lgessler 7y agoOne is a function, one is a call to that function. I think this is just hard to grasp because in other languages there's often not occasion to use a function pointer. But compare these two code snippets in Python and Scheme: >>> def add_one(x): ... return x + 1 ... >>> add_one <function add_one at 0x7f7504bf2050> >>> add_one(1) 2 >>> list(map(add_one, [1,2,3])) [2, 3, 4] That's the same as: => (define (add-one x) (+ x 1)) add-one => add-one #[compound-procedure add-one] => (add-one 1) 2 1 ]=> (map add-one (list 1 2 3)) (2 3 4)
- nickloewen 7y agoWhen 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!
- Viliam1234 7y agoWriting "x" in Lisp is like writing "x" in JavaScript. Writing "(x)" in Lisp is like writing "x()" in JavaScript, that is executing the function which was stored in the variable x.
- taeric 7y agoStrictly, this isn't right either. For example, (defun Foo (x) ... That is not calling a function x. Similarly, (let ((x 4))... Also not calling a function. It is just that, in lisp, you start a list of data with paren. In other languages, you activate a different part of the parser. It so happens that the default thing the evaluator does to a list is to apply it to a function given in the first spot.
- dreamcompiler 7y agox means "the symbol whose name is x". Symbols are first-class data structures in Lisp; they are basically C structs with a predefined structure, one of whose fields is called "name" which contains a string. (The other fields are unimportant for this discussion.) Symbols are looked up by their name, so there can only be one symbol whose name is "x" (not completely true but true enough for this discussion). (x) means "a list that contains the symbol whose name is x." Lists are also first-class objects in Lisp, and they are built using cons cells. A cons cell is itself a struct with precisely two fields: a CAR and a CDR. And you can put anything in either field, including a pointer to another cons cell. The x symbol in this example would be pointed to by the CAR of a cons cell, and the symbol NIL (a special global symbol) would be pointed to by its CDR to indicate "the end of the list." So (x) means a struct whose CAR contains a pointer to the symbol named "x" and whose CDR contains a pointer to the symbol named NIL. Otherwise known as a list with one element.