2

I was curious that are there any ternary operator being used in programming language except ?: operator. And could found only 2 from wikipedia

Is it only operator we have been used? Are there any more than these?

Thaina Yu
  • 1,372
  • 2
  • 16
  • 27
  • 1
    [In Swift, you can kind of declare as many ternary operators as you like.](https://stackoverflow.com/a/26638811/5133585) – Sweeper Aug 26 '19 at 08:02
  • They are bug factories. The result type of a unary or binary operator is unambiguous but not for a ternary operator. So either the compiler has to complain with an opaque error message that the 2nd and 3rd operand don't agree or, commonly, has to apply a conversion to force them to be the same. A conversion that rarely does what the programmer intended. The conditional operator was hard to stop, snuck in through BCPL, a very weakly typed language. – Hans Passant Aug 30 '19 at 11:35
  • 1
    Not sure if you're looking for different use cases of ternary operators or for different "syntaxes" of ternary expressions? If the latter, Python's [Conditional Expressions](https://docs.python.org/3.6/whatsnew/2.5.html#pep-308-conditional-expressions) (`x = true_value if condition else false_value`) is an example of an alternative syntax to the C-like `?:` ternary conditional expression. – D Malan Sep 04 '19 at 14:37
  • @DelenaMalan I want to know other kind of ternary operator that is not `if` `then` `else`, not the `?:` operator. But the distinct operator that require 3 arguments with 2 symbols to produce the result. As in wiki https://en.wikipedia.org/wiki/Ternary_operation#Computer_science has example of SQL `between` operator – Thaina Yu Sep 04 '19 at 16:41

3 Answers3

3

Element update

Another useful class of ternary operator, especially in functional languages, is the "element update" operation. For example, OCaml expressions have three kinds of update syntax:

  • a.b<-c means the record a where field b has value c
  • a.(b)<-c means the array a where index b has value c
  • a.[b]<-c means the string a where index b has value c

Note that these are not "update" in the sense of "assignment" or "modification"; the original object is unchanged, and a new object is yielded that has the stated properties. Consequently, these operations cannot be regarded as a simple composition of two binary operators.

Similarly, the Isabelle theorem prover has:

  • a(|b := c|) meaning the record a where field b has value c

Array slice

Yet another sort of ternary operator is array slice, for example in Python we have:

  • a[b:c] meaning an array whose first element is a[b] and last element is a[c-1]

In fact, Python has a quaternary form of slice:

  • a[b:c:d] meaning an array whose elements are a[b + n*d] where n ranges from 0 to the largest value such that b + n*d < c

Bash/ksh variable substitution

Although quite obscure, bash has several forms of variable expansion (apparently borrowed from ksh) that are ternary:

  • ${var:pos:len} is a maximum of len characters from $var, starting at pos
  • ${var/Pattern/Replacement} is $var except the first substring within it that matches Pattern is replaced with Replacement
  • ${var//Pattern/Replacement} is the same except all matches are replaced
  • ${var/#Pattern/Replacement} is like the first case except Pattern has to match a prefix of $var
  • ${var/%Pattern/Replacement} is like the previous except for matching a suffix

These are borderline in my opinion, being close to ordinary functions that happen to accept three arguments, written in the sometimes baroque style of shell syntax. But, I include them as they are entirely made of non-letter symbols.

Congruence modulo

In mathematics, an important ternary relation is congruence modulo:

  • a ≡ b (mod c) is true iff a and b both belong to the same equivalence class in c

I'm not aware of any programming language that has this, but programming languages often borrow mathematical notation, so it's possible it exists in an obscure language. (Of course, most programming languages have mod as a binary operator, allowing the above to be expressed as (a mod c) == (b mod c).) Furthermore, unlike the bash variable substitution syntax, if this were introduced in some language, it would not be specific to that language since it is established notation elsewhere, making it more similar to ?: in ubiquity.

Excluded

There are some categories of operator I've chosen to exclude from the category of "ternary" operator:

  • Operations like function application (a(b,c)) that could apply to any number of operators.
  • Specific named functions (e.g., f(a,b,c)) that accept three arguments, as there are too many of them for any to be interesting in this context.
  • Operations like SUM (Σ) or let that function as a binding introduction of a new variable, since IMO a ternary operator ought to act on three already-existing things.
  • One-letter operators in languages like sed that happen to accept three arguments, as these really are like the named function case, and the language just has a very terse naming convention.
Scott McPeak
  • 8,803
  • 2
  • 40
  • 79
  • Thank you very much. Though I would argue that I would consider some of your example such as `<-` is actually is binary. But slice:count:step and find/replace is actually one great example of ternary operator – Thaina Yu Sep 05 '19 at 04:39
  • I just realize that what I actually expect is a function that require 3 arguments and is very important to the point that we made it into distinct operator of language, and then I want to know what the syntax of it. Conditional ternary `?:` is already well known but other than that is hard to see – Thaina Yu Sep 05 '19 at 04:47
  • @Thaina You might be misunderstanding the meaning of `<-`. It is not like assignment; the original object is unchanged, and it could not be replaced by a composition of two binary operators. I've clarified my answer. – Scott McPeak Sep 05 '19 at 10:39
  • What I understand is, with syntax `a.b<-c`, the `.` is not actually tied to `<-`. The left side could be any kind of query syntax and so I consider that query syntax itself is wholly one argument – Thaina Yu Sep 06 '19 at 03:17
  • 1
    @Thaina It looks that way, but it's not. If you follow the link to the grammar, you see three ternary productions with `<-`, the ones I listed. That's because those are the only cases in OCaml where it is sensible to construct a new thing that is like an old thing except with one element different. In a functional language, like in mathematics, there is no "address of", so you must simultaneously reference the old thing, the element, and its new value. Within that language, this operation *must* be ternary. (There is also a binary `<-` form that does act like assignment.) – Scott McPeak Sep 06 '19 at 03:42
0

Well it’s not a ternary operator per-say but I do think the three way comparison operator is highly underrated.

Jon Koelzer
  • 350
  • 2
  • 9
0

The ternary operator is appropriate when a computation has to take place, even if I cannot use the effect inside of an if/else statement or switch statement Consequently, 0 or the DEFAULT VALUE is treated as a DEFAULT VALUE when I try the computation.

The if/else or switch statements require me to enumerate every case that can take place and are only helpful if the ability to discriminate between a numeric value and a branching choice can help. In some cases, it is clear why it won't help if a condition test exists, simply because I am either too early or too late to do something about the condition, even though some other function is unable to test for the given condition.

The ternary operator forces a computation to have the effect that can pass through the code that follows it. Other types of condition tests resort to using, for instance, the && and || operators which can't guarantee a pass through.

C. R. Ward
  • 93
  • 5