Search a sequence for its minimum and stop as soon as the lowest possible value is found

Jussi Piitulainen jussi.piitulainen at helsinki.fi
Sat Jan 7 03:54:35 EST 2017


Chris Angelico writes:

> On Sat, Jan 7, 2017 at 7:12 PM, Jussi Piitulainen wrote:

>> You switched to a simpler operator. Would Haskell notice that
>>
>>    def minabs(x, y): return min(x, y, key = abs)
>>
>> has a meaningful zero? Surely it has its limits somewhere and then
>> the programmer needs to supply the information.
>
> If the return value of abs is int(0..) then yeah, it could. (Or
> whatever the notation is. That's Pike's limited-range-int type
> syntax.)

Maybe so. If Haskell abs has such types. (For integers, rationals,
whatever numeric types Haskell has, which I've quite forgotten, or it
may have even changed since I knew some Haskell. It's been a while.)

I rewrite the question so that the answer cannot be deduced from just
the types of the functions:

    def minabs(x, y): return min(x, y, key = lambda w: max(w, -w))

Surely max of two ints is an int. Maybe the Haskell compiler could
specialize the type, but my question is, is it _guaranteed_ to do so,
and how should the programmer know to rely on that?


More information about the Python-list mailing list