9 ms·
The empty list
- deleted 4y ago[deleted]
- djha-skin 4y agoIt's very difficult to deserialize in serialize things like yaml and json in CL because it doesn't have a distinct false object. When you do deserialize `false` it turns into nil. Then when you serialize it back again it turns into `null`. From a practical perspective, this is annoying. I don't hate using it as a language, it's fine, but because it's different enough than other languages it confuses people.
- lisper 4y agoI share your frustration, particularly since one could add a canonical false value to CL in a backwards-compatible way. But it's too late now. 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.
- kmstout 4y ago> you could de-serialize false into, say, :false ... 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 https://github.com/marijnh/ST-JSON
- lisper 4y agoYeah, good point. IMHO the lack of a canonical serialization for hash tables is a much more serious omission than not having a separate boolean false value.
- thomastjeffery 4y ago> in a backwards-compatible way. But it's too late now. You just contradicted yourself.
- lisper 4y agoNo, that's not a contradiction. To add a canonical false value you would have to change the semantics of the language, which means you have to change the standard. In general changing standards is possible, but in the case of CL it is not possible for practical reasons: changing standards costs money and no one would fund such an effort. CL is moribund, not unlike Smalltalk, APL, and COBOL.
- ykonstant 4y ago>CL is moribund, not unlike Smalltalk, APL, and COBOL. If someone wants to do some hobby/small-scale but complex programming in LISP, what kind of dialect should they use? Scheme/Racket or something else?
- lisper 4y agoPersonally I do all my personal coding in CL but it's a matter of taste. Another word for "moribund" is "stable" and that can be a good thing. But it really is mostly personal preference. And you might want to add Clojure to your list of languages to consider. The reason I like CL is that I find it is a local optimum impedance match for my brain in language design space. But that is at least in part because I have been using it for 35 years so I am used to its quirks (of which there are many). But it also has lots of really nice features, some of which are still unique to CL and which I find indispensable to my coding style. Generic functions are at the top of that list, and a close second is the macro system.
- baq 4y agoI'm afraid you're confusing 'stable' with 'stagnant'. (The dictionary suggests 'dying'... I won't go there ;))
- shadowgovt 4y agoYep. It's probably the case that one should have a more complicated serializer / deserializer for JSON in CL (one that takes into account the expected JSON datatypes) because JSON is yet another type and value system from Scheme's and CL's, but there's an isomorphism from the Scheme one to the JSON one and there's no such similar isomorphism for the primitive CL types (though were one so inclined, one could certainly build one in one's own application). 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.
- bombolo 4y agoYou want empty lists because you can't iterate a null. So an empty list should be an empty list, otherwise it will require special code to check for the null case.
- shadowgovt 4y agoDo you mean you can't iterate a null in JavaScript or in LISP? 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.
- bombolo 4y agoWell if you could iterate a null, you'd also be able to sum 2 nulls. And you'd need null to carry a length (of zero, I presume).
- shadowgovt 4y agoCorrect. Which could, of course, all be defined. - null + null === null ; list + null === list; null + list === list - null.length === 0 /* should probably make it a runtime error to write this property, or allow it but have the result be a list with length elements, all undefined */ (Note: somewhat hilariously, null + null is already defined in JavaScript. It, of course, is 0. Wat. ;) ) Whether null and empty list should be different or the same is an old argument, and both directions lead to different problems. http://thecodelesscode.com/case/6 http://thecodelesscode.com/case/6
- stassats 4y ago> it's different enough than other languages it confuses people. 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.
- User23 4y agoI agree that it’s annoying. It is possible to work around without too much trouble by defining some other constant that evaluates to nil to distinguish.
- rhn_mk1 4y agoSerialization formats not designed specifically for the language should not be treated as serializers for objects in that language. 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.
- scatters 4y agoJSON is the JavaScript Object Notation; it's in the name! The closer your language's object model is to JavaScript, the more likely it is you'll get away with treating it as a direct serialization format. Conversely, the further your language is from JavaScript, the more you'll need to add types specifically to represent JSON.
- dragonwriter 4y ago> It's very difficult to deserialize in serialize things like yaml and json in CL because it doesn't have a distinct false object. 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.
- lisper 4y ago> It is necessary that there be a special empty-list object which is a list but not a cons. 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.
- rosebay 4y agoI tend to agree but would be happy to be proven wrong by more knowledgeable folks in these comments. However I assume here we are talking about dotted lists and not ‘proper’ ones?
- lisper 4y agoNo. Dotted lists are just a notational convention. All lists can be written in dotted notation. 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.
- mtreis86 4y agoIn other words CL-USER> (equalp '(1 2 3) '(1 . (2 . (3 . nil)))) => T
- creepycrawler 4y agoYou are conflating implementation tricks with language semantics. In Lisp, NIL is always an atom, never a cons. Also, 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))).
- tuukkah 4y agoTLDR (at the end): "Certainly the CL approach carries more historical baggage." > 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.
- lisper 4y agoThat's one reason. Another is that it allows you to use the idiom (if X ...) to test for non-null rather than (if (not (null X)) ...)
- tuukkah 4y agoIndeed, although some languages achieve the same by defining multiple falsy values, e.g. JS has 9 such values: https://developer.mozilla.org/en-US/docs/Glossary/Falsy https://developer.mozilla.org/en-US/docs/Glossary/Falsy
- lisper 4y agoYeah, but this causes its own set of problems when, for example, you are surprised that 0.0 is false but -0.0 is true. In general, punning of any kind is a Really Bad Idea when code gets complicated.
- ruricolist 4y agoPersonally I feel about Scheme's distinction between false and the empty list the way Schemers feel about CL's distinction between variables and functions.
- bjoli 4y agoWhy? It is largely inconsequential. You save a line or two, but there are bigger fish. Clos, contitions and restarts, and types change how you actually write code. Nil being false doesn't mean anything. You can save a line of code when iterating over a list, which is handy, but it lacks meaning. Multiple namespaces are a nuisance for about one day until you have gotten used to them.
- gumby 4y agoI like that the article was non judgement. The real world is messy and Common Lisp formalized existing practice among several branches of an already old language from before programming language theory. 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."
- bhk 4y agoTo help someone learn to use a language, we should be judgemental and call special attention to all of its shortcomings and gotchas. I don't see value in sweeping them under the rug. That kind of language advocacy is a zero-sum game (or negative-sum). 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.
- mik1998 4y agoI don't see the benefit of the Scheme approach at all. As far as I'm concerned, the empty list being also the false object makes certain list functions more elegant to implement and it is mostly irrelevant elsewhere.
- NoboruWataya 4y agoI've been trying to learn CL by using it to do AOC. As someone coming from mostly Python, there is a lot about it that confuses and, occasionally, frustrates me. 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.
- aidenn0 4y agoLists in python are what are called vectors in almost every other language. You can make a resizable vector in CL and use vector-push-extend to do things just like python.
- shadowgovt 4y agoIn LISP, the cheap / straightforward operation is attaching things to the front of a list. 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).
- martinflack 4y agoAPPEND is for concatenating two lists. If you're trying to add a value, then if your NIL is already in a variable, use PUSH to add the value (to the front). This will build the result list in reverse, so you can NREVERSE it at the end.
- User23 4y agoThere’s a relatively straightforward deque implementation too if you need to append in addition to pushing. It can require a little rebalancing, but I imagine it amortizes to constant time.
- tuukkah 4y agoappend concatenates lists so you'd need (append nil '(foo))
- JoelMcCracken 4y agoI like the way scheme does it, but IMO this is one of the least significant reasons scheme choices are much preferable to cl imo. It’s just a much cleaner, friendlier language overall
- tmtvl 4y agoI really like Scheme, though the Common Lisp type system has completely won me over (especially with defstar).
- cratermoon 4y agoThe Null Object[1] is a useful thing. It can represent the behavior when there's no correct behavior. It exists in languages like SmallTalk as well as LISP and LISP-derived languages. In Go, you can call methods on nil objects. [2] 1 https://wiki.c2.com/?NullObject https://wiki.c2.com/?NullObject 2 https://go.dev/play/p/mgayNTjdacL https://go.dev/play/p/mgayNTjdacL
- neilv 4y agoAn annoying thing about `nil` is when it stomps a namespace in which you're using symbols, such as in a minilanguage, by being read as the nil value rather than as the symbol `nil`. 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.
- pfdietz 4y agoYou should not have been reading that in a package that inherited from COMMON-LISP. Did you really want user names that happened to be COMMON-LISP symbols to collide?
- neilv 4y agoThat time was in Emacs Lisp. And it was very rapid-convenient to be using symbols. 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).
- pfdietz 4y agoThe section on the `t` value could also have added that in the Common Lisp standard, there are a great many functions that returns a boolean value. When they return a true value, all that is required is that they return a value that isn't nil. This could be `t`, and often is, but is not required to be. 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>))`.
- deleted 4y ago[deleted]
- kazinator 4y agoI distinctly remember from over two decades ago that, as I was exploring Lisp under the guidance of Shapiro's book Common Lisp: An Interactive Approach, the situation with nil being a symbol, empty list and false was simply a fantastic design, and it likely helped me get hooked on Lisp. 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".