3 ms·
> Pointers to TWO pointers (first pointer is named car, and the 2nd pointer is named cdr). This is not true; only cdr is a pointer to a pointer. car is a point
by xdavidliu 4y ago
> Pointers to TWO pointers (first pointer is named car, and the 2nd pointer is named cdr).
This is not true; only cdr is a pointer to a pointer. car is a pointer to a value. For example, if x is (1 2), then (car x) is a pointer to the value 1, while (cdr x) is a pointer to the list (2), so only the latter is "to a pointer".
> convert your C code into a vector or a hash-map.
nit: C has neither vectors nor hash-maps, neither builtin nor part of any standard library.
- dragontamer 4y ago(car [(This example) of a list]) == (This example), which is a pointer.
- xdavidliu 4y agothat's an example, sure, but the original claim was that "lisp lists have two pointers to pointers", which certainly is not always true.
- dreamcompiler 4y ago> only cdr is a pointer to a pointer. This isn't really true either. Either half of a cons could contain a pointer or a direct value. Lisp makes the decision about pointers based on whether the value you're trying to store is small enough to fit there directly or not. And crucially, Lisp can tell the difference between a pointer and a value at runtime which means it will always do the right thing with whatever it finds. This is why it gets dicey talking about pointers in Lisp: Pointers exist all over the place in Lisp but you have no direct control over them. That's the garbage collector's job. Nothing in Lisp requires pointers in either half of a cons cell. There is a convention followed by the Lisp reader and printer that lists are represented with their head in the car and their remainder in the cdr of a cons cell, but the cons cells themselves don't care. Cons cells themselves know nothing about lists. They're just dumb 2-tuples with a car half and a cdr half. You're free to stick anything you want in either side. Each side holds 64 bits, and if you want to stick a string containing the Gettysburg address in the car [or cdr], Lisp will let you. (More specifically if you create this string Lisp will allocate a pointer for it, and it will then stick that pointer in the car.)
- xdavidliu 4y ago> Nothing in Lisp requires pointers in either half of a cons cell. You are definitely right about that, but the initial claim was made about "Lisp list", for which cdr is certainly a pointer. I would expect that when we refer to "Lisp lists", we certainly don't include things like (cons 1 2)
- dreamcompiler 4y agoExcept at the end of the list when the cdr contains nil, which is not a pointer but a special value. BTW (listp (cons 1 2)) => true so (cons 1 2) is indeed a list; it's just not a proper list.