5 ms·
"p" makes sense if you know that it stands for "predicate". It's not nearly as arcane as car and cdr. Also can we finally settle on a name for these things now
by Smithalicious 7y ago
"p" makes sense if you know that it stands for "predicate". It's not nearly as arcane as car and cdr.
Also can we finally settle on a name for these things now? If car and cdr are too arcane we can do away with them (though I like being able to do caddadadr) but why do we need "first" and "rest" rather than the already established "head" and "tail"?
I particularly dislike "rest" since it's a relative term, i.e. in normal speech saying that you're going to do something with "the rest" of the list means something different depending on how much of the list you already used.
- puredanger 7y agoIn Clojure, first and rest apply to sequences, which are a logical list abstraction. They apply to lists, but they can also be used on sequential views of indexed vectors, maps, sets, database result sets, files in a dir, lines in a file, and an open world of "things that can be seen in some order". head and tail I suspect are much more tied to linked lists and data structure than the more plain first and rest.
- namibj 7y agoI might add that breaking the naming away (from traditions) also helps breaking the mindset away (from "that's how we always did it"). This allows for unified abstract views on things normally distinguished from each other by words, syntax, conventions and mind. This allows stuff like pattern matching generic functional code like the mighty monad with it's transformer. Learning functional programming incl. higher kinded types (dependent types and such) isn't easy for someone used to e.g. Java,C,C++,Python,Go, and others I can't judge of the top of my head. If you ask yourself why said functional is beneficial, remember the popularity and typical confusion of the Monad posts/articles. You might not quite understand / "get" it, but they keep coming up and the authors seem intelligent. So you (might) just have a case of the Blub Paradox[0]. You need to understand it to grok it's usefulness/desirability. [0]: http://www.paulgraham.com/avg.html http://www.paulgraham.com/avg.html
- lonelappde 7y agoIf you are using first and rest, then you are treating whatever sequence you have as a linked list, so you've already lost the plot.
- greggyb 7y agoYou can treat any collection or data structure that implements the sequence protocol as a lazy sequence with (seq collection) But you don't have to. If you choose to, you're not tied to a representation of mutable cons cells. There are good reasons to want to iterate over values in a map or a set, while still wanting the lookup characteristics of those collections, rather than a linked list. This is hardly a controversial stance. Python, for example, supports iteration over many of its data structures and it is recommended to implement the dunder methods for iteration on any class that might reasonably be the basis of an iteration. All clojure does is agree that iteration over a lazy sequence is a valuable concept. It implements this as an abstract protocol, rather than as a concrete implementation on linked lists.
- greggyb 7y ago> "p" makes sense if you know that it stands for "predicate". It's not nearly as arcane as car and cdr. We are in agreement here. It seems to me, though, that it is an easy case to make that a question mark makes sense if you know English. Surely this is a convention less dependent upon specific knowledge. Either way, Clojure is its own language and is not beholden to the tradition of any other language. What is most important is that it chooses a convention and implements it consistently. It is not meant to be Common Lisp nor is it meant to be Scheme, nor Dylan, nor Shen, nor Femtolisp, nor any other implementation. It is meant to be Clojure and what that means is defined by Rich Hickey. Regarding`first`, `rest`, `head`, and `tail`, I don't think the argument based on English usage is the strongest. `head` and `tail` are established in some other programming languages, almost always as operations upon a link list. Those two words in English refer to body parts on some animals, hardly an obvious analog to generic collections. `first` and `rest` are not unheard of in other programming languages and in English do have a natural connection with the idea of a collection. Again, Clojure is meant only to be what Rich Hickey designed it to be, not a faithful reproduction of any other programming language.
- lispm 7y ago> `first` and `rest` are not unheard of in other programming languages Like Common Lisp, which has `first`...`tenth` and `rest`.
- kazinator 7y agoWe should strive to name predicate functions using adjectives or verb participles. Then they do not require a suffix. (if (closed handle) ...) (when (static widget) ...) The p or -p or ? should only be used in situations when we can't avoid naming the predicate after a noun, like stringp for "is it a string".
- Smithalicious 7y agoI don't like this since I prefer using adjective for conversions/casts, i.e. I'd like (int x) to mean "x considered as integer". If you mix the two conventions (like you seem to suggest with your last paragraph) then it just becomes ambiguous.
- kazinator 7y agoint is an adjective?
- jhbadger 7y agoAlthough Scheme's question mark for predicates has had influence in other languages -- besides Clojure, it shows up in Ruby, for example. And for head/tail versus first/rest, I don't really mind either one, but head/tail is pretty strange if you haven't seen it already -- it makes the programmer imagine that their data is some sort of animal that needs to be beheaded (or is it supposed to to be the head and tail of a coin?)