90 points · by todsacerdoti · 3y ago · 105 comments
There are a number things worth “fixing” and it’s delightful that so much interest and implementation in this regard has been sustained for so long.
But to the ideologues, I can only quote Emerson: "A foolish consistency is the hobgoblin of little minds, adored by little statesmen and [programming] philosophers and divines."
For example:
> ... it is not at all strange that there is an object whose type is both list and symbol.
Actually, it is strange, and unfortunate. One should think about examples of code that might store different types of values in a variable or data structure, using e.g. `symbolp` and `listp` to distinguish the different representations, and consider how the collision between the symbol nil and empty list could produce unexpected behavior. One must be aware and remain vigilant.
> the things which are not necessary are that it be a symbol, and that it represent falsity.
Other non-necessary things: `car` and `cdr` special cases for `()`, self-evaluating `()`.
> CL requires precisely one implementationally-weird object, while Scheme requires two, or three if you count #t
There is nothing intrinsically weird about having an empty list object. The only thing weird about it is the behavior of `car`, `cdr`, and `symbolp` in CL.
Also, there is nothing weird about having a proper boolean type. On the other hand, if CL's `(type-of 't)` actually returns `boolean` and not `symbol`, as the article indicates, then that is indeed weird.
That's kind of my point.
Multiple namespaces are a nuisance for about one day until you have gotten used to them.
Besides it being obvious, I actually ran into this in a real-world situation, with some super-rapid work on a dataset of only about 100 entries, where it turned out one user's real name had the initials N.I.L.
When I moved to Scheme, I liked that it didn't have that particular collision. Though quasiquoting had some collisions unnecessarily (when they already had precedent of using the `#` character to introduce special values).
There are only a few forms that are required to return `t` for true, calls to the functions `not` and `null` being two of them. So, to coerce a true to `t`, one does `(not (not <form>))`.
Maybe I'm just missing something obvious but it doesn't seem like you can start with an empty list that you can repeatedly append to. The empty list is nil, and `(append nil foo)` seems to just yield `foo`, not `(list foo)`. So trying to append to that object will (probably) raise an error.
Appending to the rear of a list is (usually) going to be the more expensive operation (naively, doing so requires walking all the way to the end of the list, because lists are more like linked list constructs than arrays that silently resize as needed).
(let ((some-place nil))
(push an-item some-place)
...
(nreverse some-place)) ;; if the order really matters, you see the same pattern in Erlang and others
Pushing (and popping) work on the front of lists. Counterintuitively, vector-push and vector-pop work on the end of the vector (though the rationale around performance is the same,). By pushing/popping on the front of the list the action can be done in constant time. `push item place` is equivalent to this whether it's precisely this under the hood for an implementation or not: (setf place (cons item place))
Pop is this: (prog1
(car place)
(setf place (cdr place)))
(Maybe I've been spending too much time in the hyper spec, my equivalence examples could have been copy/pasted from it and I wrote them before double checking.)What you describe is what a human typical would write.
For tools this is optimized. For example in (loop for e in list collect (oddp e)) this usually would be optimized. It would not add to the end and then reverse. Instead, it would keep a pointer to the end and add to the end via that pointer.
(let* ((list (list 1 2 3 4))
(last (last list)))
(rplacd last (list 5 6))
(setf last (last last))
(format t "list: ~a~%last: ~a~%" list last))
=> list: (1 2 3 4 5 6)last: (6)
(cons 'foo nil)
It's in the oposite order. It starts with the empty list and it adds foo in the head.An extension is
(cons 'bar (cons 'foo nil))
that is equivalent to (list 'bar 'foo)You can make a resizable vector in CL and use vector-push-extend to do things just like python.
You can, but appending some list L and an empty list, or an empty list and L yields L in either case; there's nothing to append. Subsequent appends will be more interesting, e.g.
CL-USER> (append '(x) (append '(y z) '()))
(X Y Z)
> The empty list is nil, and `(append nil foo)` seems to just yield `foo`, not `(list foo)`Right. Conceptually APPEND appends lists and not bare elements; try (append '() '(foo))
It isn't. It has none of the mechanics of an empty list in Python etc.
It's more like a null link, and has quite a bit in common with /0 as a string terminator.
It does different things on its own and in the context of a list.
The key is that Lisp list items are stored as car (link/pointer to an item) and cdr (link to the next item/s in the list) pairs.
Lisp's dot notation makes this explicit, but it's hidden behind syntactic sugar because it's messy and hard to read.
(a b c) is really (a . (b . (c . NIL)))
Each dot shows the cdr of the preceding car.On its own NIL is just a constant. It has no listy features - specifically no slots for car or cdr values. Although you can do things like (length NIL) you can't change it. It's simply not a list.
You can add car/cdr pointers to it with cons. Now you can use NIL in a list.
You can use NIL as a cdr list terminator. (something . NIL) means the list is over. There are no more cdrs after it.
You can use NIL as an empty car placeholder. (NIL . something) means the car has no value, but the list continues onwards with a cdr link.
In your example (append NIL foo) actually returns (foo . NIL). NIL is being used as a cdr terminator. Lisp hides the ". NIL" because syntactic sugar.
If you (append NIL foo) again, you still get (foo) because NIL already terminates the list and there's no reason to add another NIL after it.
(push foo NIL) throws an error because push is destructive and is attempting to change NIL. Which isn't allowed.
(cons NIL a) gives you (NIL a) because NIL is being prepended as an empty car pointer. Which is how you get NILs into a list without it collapsing around itself. It's just like any other list item, except it has a null value. The list can continue past it into further cdrs in the usual way.
Anyway. This is confusing because you have a single symbol doing three different things in three different contexts. (Not even counting its use as a Boolean...) Worse, syntactic sugar and the various function internals hide this from you. And the documentation tells you something that isn't true.
So unless you look at the source and/or learn how the various functions understand and use NIL you will be confused.
The up side is a REPL is interactive and easy to play with, so it's not a huge effort to experiment and see what falls out.
I can see how NIL is confusing when coming from Python, but I think this comment has it backwards. The idea of lists as chains or trees of pointers with a distinguished termination value predates LISP by a few years. Moreover, this kind of list predates Python's use of the term by more than 35 years. If you treat lists as just a particularly useful subclass of trees of CONS cells, then they and NIL make perfect sense. Python's lists are fine too, they're just a different kind of thing.
That is not true. NIL could be a CONS. In fact, it acts just like a CONS whose CAR and CDR are itself. The only way in which NIL does not behave like a CONS is that it does not answer true to CONSP. But that doesn't matter because it answers true to NULL, so you can build a CL-style CONSP if you want it by checking for (AND (CONSP thing) (NOT (NULL thing))).
It is not even necessary that NIL be unique. The only thing that is necessary is that there be some non-empty set of distinguished objects with a predicate to test for membership in that set which by convention designate the ends of lists.
However I assume here we are talking about dotted lists and not ‘proper’ ones?
You can define NIL as follows:
(setf NIL (cons 0 0)))
(setf (car NIL) NIL)
(setf (cdr NIL) NIL)
Then: (defun null (thing) (eq thing NIL))
You also have to add a bunch of special cases to other functions: (defun cl-style-consp (thing)
(and (consp thing) (not (null thing)))
(defun cl-style-symbolp (thing)
(or (symbolp thing) (null thing))
(defun cl-style-symbol-name (thing)
(if (null thing) "NIL" (symbol-name thing)))
and a few others.In fact, many CL implementations actually implement NIL that way so that CAR and CDR of NIL return NIL without having to make that a special case.
CL-USER> (equalp '(1 2 3)
'(1 . (2 . (3 . nil))))
=> TAlso, a dotted list is a nonempty list where the cdr of the last cons is not NIL. It is not a notational convention. A list (a b c) is not a dotted list, even if you write it as (a . (b . (c . nil))).
My view today is that if anything calls itself Lisp and doesn't have these design elements, it is, in a way, vandalizing the word "Lisp".
In Go, you can call methods on nil objects. [2]
I don't hate using it as a language, it's fine, but because it's different enough than other languages it confuses people.
Its not particularly hard to represent JSON’s type system in CL and deserialize JSON to CL objects.
Its also not particularly hard to construct and use a model of CL data in JSON from CL. (Its even easier with YAML.)
OTOH, yes, JSONs data model is almost exactly JS’s data model (minus anything callable, which js a lot of JS, and with a slightly different model of numbers, but anything that doesn’t exactly match JS’s model is canonically unreliable and so discouraged), So with any thing other than JS using JSON, you have to determine if your use case is modeling your language’s data in JSON or JSON’s data model in your language, while with a particular subset of JS you can just ignore the distinction.
That is, JSON is not a representation of objects in your language. It's your application that holds a representation of JSON data. If you're not paying attention to the translation layer between your native objects and the JSON ones, you'll hit problems sooner or later.
But as a practical solution you could de-serialize false into, say, :false, in order to distinguish them from empty lists, and then do a post-processing step to turn those into nils if you wanted to.
That's the approach that st-json [0] builds on:
- true and false become :true and :false, respectively
- [] becomes nil
- {} becomes #s(st-json:jso :alist nil)
---
[0] in QuickLisp and at https://github.com/marijnh/ST-JSON
You just contradicted yourself.
I'd be willing to argue that this is indicative of JSON's unnecessary complexity (inherited from its source language... why does JavaScript need a false and an empty array and a null and even an undefined?), but reasonable developers can disagree on that point.
Why does Lisp need a 0 and a nil? Because numbers are conceptually different from lists, and the number 0 is conceptually different from no number at all.
Why does JavaScript need a false and an empty array and a null and an undefined? Because all of those represent different concepts. Trying to stuff as many concepts as possible into a single representation doesn't necessarily make a language better (though I'm certainly not claiming that JavaScript is the epitome of good language design).
You can't iterate a null in JavaScript, but that's a design decision of the language that could have gone another way; `for (const elem of null) {do();}` could have been specified to be valid JavaScript that never calls `do()`, but it wasn't because it wasn't.
https://stackoverflow.com/questions/13796751/json-undefined-...
And javascript integers (or lack thereof) don't match CL integers. So if you want to interoperate between distinct languages you do have to consider the differences anyway.
> Combining the empty list and the special false object can lead to particularly good implementations perhaps.
I suppose they wanted the null pointer to represent them both in RAM, because every CPU has an instruction to test whether a value is zero, or even a flag that is set for free when you encounter the value.
In general, punning of any kind is a Really Bad Idea when code gets complicated.