Here's constructions in several languages. See if you can understand what they do and identify each language.
(floor x y)
require POSIX; floor(x/y);
They are all doing
In Common Lisp
(floor (/ x y)) can be written as
(floor x y).
Because Common Lisp's “floor” function essentially acts as a integer quotient function.
The second argument is optional, with default value of 1.
(see spec: Source www.lispworks.com.)
(floor x y) is not a good lang design, more of a lang quirk.
Similarly, in Python 2.x,
will work when both x and y are integers. Also,
works too, but that
// is a unreadable syntax quirk.
Similarly, in perl, you can do
require POSIX; floor(x/y);.
The “POSIX” instead of “Math” is a quirk. In any case, “floor” should really be builtin.
Another way to do this in perl is
All of the above are quirks. They rely on computer engineering by-products (such as “int”), or rely on the lang's idiosyncrasy. One easy way to measure it is whether a programer can read and understand a program without having to spend a lot time to delve into its tricks. Problem with these lang idioms is that it's harder to understand, and whatever advantage/optimization they provide is microscopic and temporary.
Best is really
Idiomatic programing is a bad thing. It was spread by perl, of course, in the 1990s. Idiomatic languages, i.e. lang with huge number of idioms, such as perl, is the worst.
Thanks to Zach Beane, Kaz Kylheku, for the tip on Common Lisp's “floor”.
For actual code of these langs using
floor(a/b) doing something useful, see: In-place Algorithm for Reversing a List in Perl, Python, Lisp, Mathematica.
See also: 〔Programming Languages Have Social Mores Not Idioms By Zed A Shaw. @ learncodethehardway.org…〕