Live data from Hacker News

I am a horse in the land of booleans

iloveponies.github.io

71–80 of 89 posts

Re: I am a horse in the land of booleans

#71
post #48

Because Java’s if does not return a value, you cannot say: return if (x This would have been a nice place to make an analogy to the question mark operator, since a java programmer would probably be familiar with it and it allows you to write: return if (x as return x

It's called 'ternary operator' because it's `?` and `:` and takes three operands.

Re: I am a horse in the land of booleans

#72

Earlier quoted context omitted.

In 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.

If 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.

You 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.

Re: I am a horse in the land of booleans

#73
post #18

Earlier quoted context omitted.

Question mark is a Scheme tradition. Clojure is a new lisp, drawing inspiration from many sources. One of Rich Hickey's goals, stated in many ways and in many of his presentations, was to design a modern lisp not bound by design decisions in old ones. That's why Clojure is not built on or directly based on any specific lisp. He has made a big point about moving on from lisp-isms such as `car` and `cdr`, which are bas…

"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 no…

> "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.

Re: I am a horse in the land of booleans

#74

Earlier quoted context omitted.

>>java programmer would probably be familiar with it And also C, C++, and Objective-C programmers. I'm sure there are more languages that use that operator that I have not mentioned here.

I have not touched PHP in the last 10 years but if memory serves it also has the ternary operator. Ok, lets try it: $ php -a Interactive mode enabled php > echo 1 == 1 ? "foo" : "bar"; foo php > echo 1 == 2 ? "foo" : "bar"; bar Looks like javascript has it too: 1 == 1 ? "foo" : "bar" "foo" 1 == 2 ? "foo" : "bar" "bar"

Be careful, php’s ternary operator precedence is different from (most) other languages’ (consult binding direction).

The use of parenthesis is highly encouraged...

Re: I am a horse in the land of booleans

#75
post #18

Earlier quoted context omitted.

Question mark is a Scheme tradition. Clojure is a new lisp, drawing inspiration from many sources. One of Rich Hickey's goals, stated in many ways and in many of his presentations, was to design a modern lisp not bound by design decisions in old ones. That's why Clojure is not built on or directly based on any specific lisp. He has made a big point about moving on from lisp-isms such as `car` and `cdr`, which are bas…

"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 no…

We 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".

Re: I am a horse in the land of booleans

#76
post #73

Earlier quoted context omitted.

"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 no…

> "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…

> `first` and `rest` are not unheard of in other programming languages

Like Common Lisp, which has `first`...`tenth` and `rest`.

Re: I am a horse in the land of booleans

#77
post #8

Earlier quoted context omitted.

0 being falsy makes no sense outside of the C family of programming languages. But there 0 is overloaded both as a number and pointer (it's a bit more complicated though, as the value of the null pointer doesn't need to be zero but assigning 0 to a pointer will assign the null pointer). In languages with more elaborate type systems, you usually have the pointer and number representation separated. 0 has no special me…

>But making zero elements evaluate to falsy is problematic. "" would be falsy as well then. In Python 0, [], and "" are all falsy. In my experience it's only been useful - why do you consider it problematic?

I'm not sure Python would do that today without backwards compatibility reasons. Python's True and False are late additions to the language, before that 0 and 1 served their functions.

I think this is one of the symptoms of the changing priority of simplicity in Python language design, at some point the design decision about 0/1 for simplicity became dated enough to change it despite being a big change and requiring backwards compatibility hacks with 0/1.

Re: I am a horse in the land of booleans

#78
post #48

Because Java’s if does not return a value, you cannot say: return if (x This would have been a nice place to make an analogy to the question mark operator, since a java programmer would probably be familiar with it and it allows you to write: return if (x as return x

>>java programmer would probably be familiar with it And also C, C++, and Objective-C programmers. I'm sure there are more languages that use that operator that I have not mentioned here.

Sure, but the article is written with Java programmers in mind and doesn't mention any of the other languages.

Re: I am a horse in the land of booleans

#79
post #71
post #48

Because Java’s if does not return a value, you cannot say: return if (x This would have been a nice place to make an analogy to the question mark operator, since a java programmer would probably be familiar with it and it allows you to write: return if (x as return x

It's called 'ternary operator' because it's `?` and `:` and takes three operands.

I don't like calling it the ternary operator because there are other ternary operators, like "x BETWEEN a AND b" in SQL.

Sometimes it's called the "conditional operator" but my experience has been that "question mark operator" is what the most people will understand.

Re: I am a horse in the land of booleans

#80
post #18

Earlier quoted context omitted.

Question mark is a Scheme tradition. Clojure is a new lisp, drawing inspiration from many sources. One of Rich Hickey's goals, stated in many ways and in many of his presentations, was to design a modern lisp not bound by design decisions in old ones. That's why Clojure is not built on or directly based on any specific lisp. He has made a big point about moving on from lisp-isms such as `car` and `cdr`, which are bas…

"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 no…

Although 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?)
Post reply on HN