Max integer and min integer in standard ml

I want to use max integer and min integer from ml, but I can’t, I saw from the library its type int option, how can I use it? I saw it here http://www.standardml.org/Basis/integer.html#SIG:INTEGER.minInt:VAL

I want to use it to create a max and min function, each method accepts a list of integers, and I have to return max and min, respectively.

I tried the following codes, but they did not work:

- fun max x = = foldr (fn (a, b) => if a > b then a else b) INTEGER.minInt x; - fun max x = = foldr (fn (a, b) => if a > b then a else b) minInt x; - fun max x = = foldr (fn (a, b) => if a > b then a else b) Int.minInt x; 

Does anyone know how to use them?

+4
source share
1 answer

Int.minInt and Int.maxInt are of type int option , not int .

This means that they can be either in the form of NONE , if there is no maximum (or minimum), or in the form of SOME v , where v is the maximum (or minimum).

There are many ways to get a value from an option.

  • Matching pattern.
  • valOf , which retrieves the value. (note that throws an exception if NONE is specified)
  • getOpt , which retrieves the value and uses the default value if set to NONE .

However, for this specific task, you can do this without the need for Int.minInt if you draw multiple input patterns. Also, be sure to consider the case when your input list is empty.

+5
source

Source: https://habr.com/ru/post/1403316/


All Articles