Hacker News new | ask | show | jobs
by taeric 846 days ago
I confess I have grown increasingly in favor of how common lisp does this. The basic `/` creates rationals. And "floor" is a multiple value return where the first value is the floor and the second is the remainder.

Granted, getting used to multiple value calls takes getting used to. I think I like it, but I also think I emphatically did not like it initially.

3 comments

Scheme in the form of R7RS does something similar, though since failure to capture all returned values is an error (unlike in Common Lisp), there's more functions involved. It also offers a choice between truncation and floor behavior, so there's `truncate-quotient`, `truncate-remainder`, `floor-` versions and for both values `truncate/` and `floor/`.
To be fair, Python's `/` creates floats, and this article is about a second operator `//`.
Continuing in fairness, lisp is a bit more involved, here. If you do `(/ 1.0 3)`, you do not get a rational. Similarly, any division that is not integer/rational there will get treated mostly as expected.

Basically, it seems as soon as you introduce a floating point number, it stays there. Which is roughly what I would expect.

In python 2, '/' creates integers, and the article was written in 2010 when python 2 was the most used by far.
from the link:

> PS. Note that I am using // instead of / -- this is Python 3 syntax

Oh. Right.
It's nice for general numerical methods but somewhat annoying for implementing integer-arithmetic algorithms.
Yeah, I think this is what I really didn't like about it at the start. Learning that you probably want `floor` if you want things to be integer took more time to learn than feels natural.