Is there a better name for generic cons?

Any serious programming language has a variety of collections in its library. Most reduce the resulting complexity by overloading the common operations for multiple types. But few include cons among the generic operations. This is partly because it's unnatural for many types - only the functional collections like lists and trees support it efficiently. But it may also be a problem that the operation has no obvious name.

Dylan calls it add, which sounds like arithmetic. Clojure calls it conj, for conjoin, which is unfortunately confusable with the complex conjugate function. C++ has push, but that's implicitly imperative. Other possibilities: extend, augment (or aug?), with, or maybe a more abstract symbol like &. Does anyone have a better idea?

4 comments:

  1. + is kind of taken for addition. It could be overloaded, but that's an even more confusing overloading than + for concatenation.

    ReplyDelete
  2. Good point! I like overloading, but I'm sure there are good general arguments against it.

    Still, it feels to me like something that should be represented by a symbolic operator rather thahn a word. So I like your idea of

    &

    or you could go for something like

    +++

    Having said all that, the main reason I suggested

    +

    was that I couldn't resist the chance to make a one-byte comment :-)

    ReplyDelete
  3. E calls it "with", and provides it for lists, maps, and sets. E.g.:

    [1, 2].with(3)
    --> [1, 2, 3]

    [3 => 'a', 7 => 'b'].with(4, 'c')
    --> [3 => 'a', 7 => 'b', 4 => 'c']

    ReplyDelete

It's OK to comment on old posts.