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?
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
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
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 suffixThese 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.
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.
There are some categories of operator I've chosen to exclude from the category of "ternary" operator:
a(b,c)
) that could apply to any number of operators.f(a,b,c)
) that accept three arguments, as there are too many of them for any to be interesting in this context.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.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.