Improving the cons operator

Here, Evan says this about infix operators:

I think all of the successful ones have two things in common:

  1. They have some visual relation to their meaning. For example, </> and <?> from the URL parsing library exactly mimic the / and ? symbols in URLs. And |> indicates directionality really clearly. The math operators like + and / are directly related to the math operators taught in every school in the world.

  2. They use the visually simple symbols. I have never seen # or % or @ or $ used to create an excellent operator. I think it is partly that they are so busy visually, mixing curves and lines haphazardly. It is also partly that they do not have super strong cultural meanings as infix operators in general. E.g. $4 means four dollars, but what is 3$4?

The cons operator (::) has the second property, but not the first. It is a symmetrical operator, so it doesn’t show directionality. And it’s not symbol from something (e.g. math or urls) that is familiar to the user of the operator. This operator is a bit confusing, especially for beginners.

Maybe it should be replaced with something like :> which shows directionality. It shows that something is being pushed to the list on the right. More importantly, it’s asymmetrical, which shows that it takes different data types on both sides.

1 Like

The (::) operator is used in many functional languages ML (SML), Scala, F#, Elixir … Haskell has a similar operator (:). So it should be somewhat familiar (1) and visually it also makes some sense:

-- these are equal
1::2::3::[]
[1,2,3]
-- this feels like less ergonomic to type (?)
-- and looks more cluttered
1:>2:>3:>[]

It shows that something is being pushed to the list on the right.

This is not what the cons operator does - it constructs a new list. (does not push to the existing one)

4 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.