1

An abstract question, not related to any particular language:

If I have a function as follows

min(int, int) :: int

which returns the smallest value in an array, and

concat([int], [int]) :: [int]

which combines two arrays, how should I write a function like

minInArray([int]) :: Int

which returns the smallest element in an array, but where the ouput could be chained like so, even with an empty input array:

min(minInArray(array1), minInArray(array2)) == minInArray(concat(array1, array2))

In other words, is there any commonly-used neutral element which minInArray could return on empty input, which wouldn't mess up min()?

mayo
  • 3,845
  • 1
  • 32
  • 42
BarbedWire
  • 189
  • 2
  • 11

4 Answers4

1

One option would be to return some neutral value like null or NaN if the array has no elements, and then if the min() function is run and one of the arguments is the neutral value, then you just return the min of the other array. Another option would be to return the closest value the language has to +Infinity if the array is empty; this works and does not require modifying min(), but does have the side effect of returning an infinite value sometimes when the minInArray() function is called. This infinite value could work as a truly neutral value that works with the default min() function, but it may cause some confusion if the minimum value in an array really is infinite.

Feathercrown
  • 2,547
  • 1
  • 16
  • 30
1

minInArray(arr1) to return null if arr1 is empty.

min() should return only non-null values over null. Meaning min() will only return null if both parameters are null. Otherwise, it will return the minimum non-null value.

pom
  • 230
  • 5
  • 17
0

While thinking about the issue we've come to seemingly the only solution possible: if an array is empty - we should return the maximum possible value for int to satisfy the condition.

Not that nice actually...

BarbedWire
  • 189
  • 2
  • 11
  • But what if both arrays are empty? Or what if one of the arrays actually contains only the maximum value for int? I'd recommend either using some sort of exception, or just checking the arrays to make sure they're not empty, before trying to calculate anything on them. – HammerN'Songs Dec 20 '18 at 16:19
  • If both arrays are empty or we just call minInArray([]) then the answer won't be nice at all. But there was a task - to find a neutral element. The same approach we can grab from Haskell's Monoid instance for Min: instance (Ord a, Bounded a) => Monoid (Min a) where mempty = maxBound – BarbedWire Dec 21 '18 at 06:03
  • I'm sure it's been used in other places, but I wouldn't write it like that if you don't have to; that'd be an annoying bug to try to track down, and would not be what I'd expect to happen if I had to figure out the code. – HammerN'Songs Dec 21 '18 at 15:38
0

Just to add some perspectives (not that this is a duplicate of the listed questions) -

All of these throw errors of some kind when asked to calculate min of an empty list or array: Java, Scala, Python, numpy, Javascript, C#. Probably more, but that's as far as I looked. I'm sure there are some that don't, but I'd expect most of those to be languages which have traded understandability and clarity for speed.

This question is about a specific language, but has answers relevant to all languages.

Note here how one can get around the issue in something like Python.

For Haskell in particular, note the advice in this question.

And lastly here's a response for a more general case of your question.

In general, it is always most important for code to work, but a close second to that is it must be understandable to humans. Perhaps it doesn't matter for your current project, if you'll be the only one dealing with that function, but the last thing I'd expect when calling a 'get_minimum' function, is Int.MAX.

I understand it makes the coding simple, but I'd urge you to beware of code that is easy to write and tricky to understand. A little more time spent making the code easy to read, with as much as possible having an immediately obvious meaning, will always save much more time later on.

HammerN'Songs
  • 201
  • 3
  • 13